flatjson converts JSON files to a "flat" representation with one value per line.For example, given the input:
{"menu" : {"id" :" file" ,"value" :" File" ,"popup" : {"menuitem" : [ {"value" :" New" ,"onclick" :" CreateNewDoc()" }, {"value" :" Open" ,"onclick" :" OpenDoc()" }, {"value" :" Close" ,"onclick" :" CloseDoc()" } ] } }} flatjson outputs:
root = { } ; root . menu = { } ; root . menu . id = "file" ; root . menu . popup = { } ; root . menu . popup . menuitem = [ ] ; root . menu . popup . menuitem [ 0 ] = { } ; root . menu . popup . menuitem [ 0 ] . onclick = "CreateNewDoc()" ; root . menu . popup . menuitem [ 0 ] . value = "New" ; root . menu . popup . menuitem [ 1 ] = { } ; root . menu . popup . menuitem [ 1 ] . onclick = "OpenDoc()" ; root . menu . popup . menuitem [ 1 ] . value = "Open" ; root . menu . popup . menuitem [ 2 ] = { } ; root . menu . popup . menuitem [ 2 ] . onclick = "CloseDoc()" ; root . menu . popup . menuitem [ 2 ] . value = "Close" ; root . menu . value = "File" ; This format, although verbose, makes it much easier to see the nesting ofvalues. It also happens to be valid JavaScript that can be used to recreate theoriginal JSON object.
This "flat" format is very handy for visualizing diffs. For example, comparingthe above JSON object with a second JSON object:
{"menu" : {"id" :" file" ,"disabled" :true ,"value" :" File menu" ,"popup" : {"menuitem" : [ {"value" :" New" ,"onclick" :" CreateNewDoc()" }, {"value" :" Open" ,"onclick" :" OpenDoc()" } ] } }} yields the diff:
--- testdata/a.json +++ testdata/b.json @@ -1,5 +1,6 @@ root = {}; root.menu = {};+ root.menu.disabled = true; root.menu.id = "file"; root.menu.popup = {}; root.menu.popup.menuitem = [];@@ -9,8 +10,5 @@ root.menu.popup.menuitem[1] = {}; root.menu.popup.menuitem[1].onclick = "OpenDoc()"; root.menu.popup.menuitem[1].value = "Open";- root.menu.popup.menuitem[2] = {};- root.menu.popup.menuitem[2].onclick = "CloseDoc()";- root.menu.popup.menuitem[2].value = "Close";- root.menu.value = "File";+ root.menu.value = "File menu";go install github.com/twpayne/flatjson/cmd/flatjson
Converting JSON to flat JSON To convert a JSON file to flat JSON, specify it on the command line, forexample:
flatjson vendor/vendor.json
If no filenames are specified, flatjson will read JSON from the standardinput.
Generating flat JSON from JSON To reverse the transformation, i.e. to convert flat JSON to JSON, specify the-reverse
option.
To generate a unified diff between two JSON files, specify the-diff
optionand the filenames on the command line, for example:
flatjson --diff ./testdata/a.json ./testdata/b.json
An additional--context
option specifies how many lines of context to show.The default is three.
MIT