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 of values. It also happens to be valid JavaScript that can be used to recreate the original JSON object.
This "flat" format is very handy for visualizing diffs. For example, comparing the 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
To convert a JSON file to flat JSON, specify it on the command line, for example:
flatjson vendor/vendor.json
If no filenames are specified, flatjson will read JSON from the standard input.
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
option
and 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