Skip to content

Latest commit

 

History

History
180 lines (103 loc) · 6.37 KB

README.e.md

File metadata and controls

180 lines (103 loc) · 6.37 KB

{{.Name}}

{{render "license/shields" . "License" "MIT"}} {{template "badge/godoc" .}} {{template "badge/goreport" .}} {{template "badge/travis" .}} PoweredBy WireFrame

{{toc 5}}

{{.Name}} - JSON Fiddling

The jsonfiddle makes it easy to look at the JSON data from different aspects.

  • jsonfiddle esc will escape any arbitrary string so as to embed it as content of json string.
  • jsonfiddle fmt will format the JSON data, either compact it or pretty printing it. The order of fields are intact.
  • jsonfiddle sort will sort the JSON data fields recursively, so that the attributes at any level are in sorted order.
  • jsonfiddle x2j means xml to json. It will convert data from xml format into json.
  • jsonfiddle j2s means json to struct. It will extract the structure of JSON data as Go struct.

Usage

$ {{shell "jsonfiddle -V"}}

$ {{shell "jsonfiddle || true" | color "sh"}}

$ {{shell "jsonfiddle esc || true" | color "sh"}}

$ {{shell "jsonfiddle fmt || true" | color "sh"}}

$ {{shell "jsonfiddle sort || true" | color "sh"}}

$ {{shell "jsonfiddle j2s || true" | color "sh"}}

$ {{shell "jsonfiddle x2j || true" | color "sh"}}

Examples

Escape with jsonfiddle esc

$ {{shell "jsonfiddle esc -i test/Customer.ref 2>/dev/null" | color "json"}}

Usage

jsonfiddle esc will escape any arbitrary string so as to embed it as content of json string. This seems useless at first, but it actually allows you to embed any arbitrary file into GitHub Gists JSON API, so as to post any arbitrary file onto GitHub Gist:

echo '{"description":"SmartyStreets API Demo","public":true,"files":{"SmartyStreets.json":{"content":'"`jsonfiddle fmt -i test/SmartyStreets.json | jsonfiddle esc -i`"'}}}' | curl --data @- https://api.github.com/gists

This will give you https://gist.github.com/anonymous/1423d4768dd9b88262ca513626e68d8e

By "arbitrary file" I do mean arbitrary file. Check this out: https://gist.github.com/anonymous/a51798ce99ff59d8d4ba536cbf4b6996

This is why jsonfiddle esc is a command on its own, instead of being part of functionalities of jsonfiddle fmt or jsonfiddle sort.

Format with jsonfiddle fmt

Pretty print

$ jsonfiddle fmt -i test/Customer.json

> {{cat "test/CustomerSI.ref" | color "json"}}

Protect templates in json data

There are times that json data may contain templates, i.e., strings like {{"{{VARIABLE}}"}}. Some of the pretty printing tools, like the json plugin in Notepad++, cannot handle such template forms well, and will turn {{"{{VARIABLE}}"}} into:

{
  {
    VARIABLE
  }
}

What's worse is that when such template variables are for int, e.g.: "age":{{"{{Var_Age}}"}}, they then wouldn't be able to handle it, for inputs like {{cat "test/CustomerP.json" | color "sh"}}

To make such template variables work for those tools, the -p,--protect option is introduced:

$ jsonfiddle fmt -p -i test/CustomerP.json

> {{cat "test/CustomerP.ref" | color "json"}}

Compact

$ jsonfiddle fmt -c -i test/Customer.json

> {{cat "test/CustomerC.ref" | color "json"}}

You can also do,

$ cat Customer.json | jsonfiddle fmt -c -i -

and the result is the same (and for all other examples using -i as well).

Sort fields with jsonfiddle sort

Sort in compact

$ jsonfiddle sort -c -i test/Customer.json

> {{cat "test/CustomerSC.ref" | color "json"}}

Sort with pretty print

$ jsonfiddle sort -i test/Customer.json

> {{cat "test/CustomerSI.ref" | color "json"}}

XML to JSON, sort then pretty print

$ {{shell "jsonfiddle x2j -i test/Books.xml | jsonfiddle sort -i - | jsonfiddle fmt -i -"}}

JSON to struct via jsonfiddle j2s

$ jsonfiddle j2s -i test/Customer.json

> {{cat "test/CustomerJ2S.ref" | color "go"}}

Purpose

A few more words on why I'm writing the tool -- because I need to compare JSON string that are roughly close and very complicated in the mean time -- sometimes even less than 30% of fields are the same, of course, this is after their having been sorted, otherwise, it'd be 100% different.

Thus all the JSON comparison tools I found are failing under such hash request. So far, I personally find that

  • Sorting the JSON data fields recursively and producing plain text file (via jsonfiddle sort), then use the state-of-the-art text comparison tools to compare them is the best approach, for my above scenario.
  • For extremely long and very complicated JSONs, converting json to abstract Go struct (via jsonfiddle j2s) is the quickest approach to compare them at higher level.

Download binaries

  • The latest binary executables are available under
    https://github.com/go-jsonfile/{{.Name}}/releases
    as the result of the Continuous-Integration process.
  • I.e., they are built right from the source code during every git tagging commit automatically.
  • Pick & choose the binary executable that suits your OS and its architecture. E.g., for Linux, it would most probably be the {{.Name}}_linux_VER_amd64 file. If your OS and its architecture is not available in the download list, please let me know and I'll add it.
  • You may want to rename it to a shorter name instead, e.g., {{.Name}}, after downloading it.

Debian package

Available at the above releases url as well.

Install Source

To install the source code instead:

go get github.com/go-jsonfile/jsonfiddle

Credits

Similar Projects

All the following similar projects have been considered before writing one on my own instead.

. . . to be filled . . .

Author(s) & Contributor(s)

Tong SUN
suntong from cpan.org

Powered by WireFrame, PoweredBy WireFrame, the one-stop wire-framing solution for Go cli based projects, from start to deploy.

All patches welcome.