little-flute

0.1.1 • Public • Published

little-flute

little-flute is a minimal dependency json parser that can be used to parse JSON objects with multiple duplicate keys.

Example Usage

const flute = require("little-flute")

new flute(
  `{
    "resource": {
      "aws_instance": {
        "example": {
          "instance_type": {
            "type": "t2.micro"
          },
          "ami": "ami-abc123"
        }
      },
      "aws_instance": {
        "example2": {
          "instance_type": {
            "type": "t2.micro"
          },
          "ami": "ami-abc123"
        }
      }
    }
  }`
).parse();

Returns the value :

{
  "resource": {
    "aws_instance": {
      "example": {
        "instance_type": {
          "type": "t2.micro"
        },
        "ami": "ami-abc123"
      }
    },
    "aws_instance_": { // duplicate keys will have an _ appended
      "example2": {
        "instance_type": {
          "type": "t2.micro"
        },
        "ami": "ami-abc123"
      }
    }
  }
}

Extensions

Users can extend the flute constructor before parse with callback functions. Each callback returns currentRef, the part of the larget object that is currently being manipluated, and parsedJson, the entire JSON object.

const flute = require("little-flute");

let extendedFlute = new flute(
  `{
    "resource": {
      "aws_instance": {
        "example": {
          "instance_type": {
            "type": "t2.micro"
          },
          "ami": "ami-abc123"
        }
      },
      "aws_instance": {
        "example2": {
          "instance_type": {
            "type": "t2.micro"
          },
          "ami": "ami-abc123"
        }
      }
    }
  }`,
  {
    onKey: function(key, currenRef, parsedJson) {
      // edit data after a new key has been added
    },
    onValue: function(value, currenRef, parsedJson) {
      // edit data when a new value is added
    },
    onOpenObject: function(key, currenRef, parsedJson) {
      // edit data after a new object has been opened
    },
    onCloseObject: function(currenRef, parsedJson) {
      // edit data after an object is closed
    },
    onOpenArray: function(currenRef, parsedJson) {
      // edit data after a new array has been opened
    },
    onCloseArray: function(currenRef, parsedJson) {
      // edit data after an array is closed
    }
  }
)

Package Sidebar

Install

npm i little-flute

Weekly Downloads

78

Version

0.1.1

License

ISC

Unpacked Size

35 kB

Total Files

6

Last publish

Collaborators

  • jvallexm