Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

flatjson converts JSON files to a "flat" representation with one value per line.

License

NotificationsYou must be signed in to change notification settings

twpayne/flatjson

Repository files navigation

PkgGoDev

Overview

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";

Installation

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.

Generated a unified diff

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.

License

MIT

About

flatjson converts JSON files to a "flat" representation with one value per line.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp