jsonfiddle

command module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2017 License: MIT Imports: 12 Imported by: 0

README

jsonfiddle

MIT License GoDoc Go Report Card travis Status

TOC

jsonfiddle - JSON Fiddling

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

  • 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 j2s means json to struct. It will extract the structure of JSON data as Go struct.

Usage

$ jsonfiddle
JSON Fiddling
Version v0.2.0 built on 2017-08-12

Tool to fiddle with json strings

Options:

  -h, --help         display help information
  -c, --compact      Compact JSON data, remove all whitespaces
      --prefix       prefix for json string output
  -d, --indent[= ]   indent for json string output
  -v, --verbose      Verbose mode (Multiple -v options increase the verbosity.)

Commands:

  fmt    Format json string
  sort   Sort json fields recursively
  j2s    JSON to struct
$ jsonfiddle fmt
Format json string

Options:

  -h, --help         display help information
  -c, --compact      Compact JSON data, remove all whitespaces
      --prefix       prefix for json string output
  -d, --indent[= ]   indent for json string output
  -v, --verbose      Verbose mode (Multiple -v options increase the verbosity.)
  -i, --input       *the source to get json string from (mandatory)
  -o, --output       the output (default: stdout)
$ jsonfiddle sort
Sort json fields recursively

Options:

  -h, --help         display help information
  -c, --compact      Compact JSON data, remove all whitespaces
      --prefix       prefix for json string output
  -d, --indent[= ]   indent for json string output
  -v, --verbose      Verbose mode (Multiple -v options increase the verbosity.)
  -i, --input       *the source to get json string from (mandatory)
  -o, --output       the output (default: stdout)
$ jsonfiddle j2s
JSON to struct

Options:

  -h, --help         display help information
  -c, --compact      Compact JSON data, remove all whitespaces
      --prefix       prefix for json string output
  -d, --indent[= ]   indent for json string output
  -v, --verbose      Verbose mode (Multiple -v options increase the verbosity.)
  -f, --fmt[=json]   the structural format of the input data (json/yaml)
  -i, --input       *the source of the input JSON (mandatory)
  -o, --output       the output (default: stdout)
      --name         the name of the root struct (default: as input file name)
      --pkg[=main]   the name of the package for the generated code
      --subStruct    create types for sub-structs

Examples

Format with jsonfiddle fmt

Pretty print
$ jsonfiddle fmt -i test/Customer.json
> test/CustomerSI.ref
{
 "address": {
  "city": "New York",
  "postalCode": "10021",
  "state": "NY",
  "streetAddress": "21 2nd Street"
 },
 "age": 25,
 "firstName": "John",
 "lastName": "Smith",
 "phoneNumber": [
  {
   "number": "212 555-1234",
   "type": "home"
  },
  {
   "number": "646 555-4567",
   "type": "fax"
  }
 ]
}
Compact
$ jsonfiddle fmt -c -i test/Customer.json
> test/Customer.ref
{
 "firstName": "John",
 "lastName": "Smith",
 "age": 25,
 "address": {
  "streetAddress": "21 2nd Street",
  "city": "New York",
  "state": "NY",
  "postalCode": "10021"
 },
 "phoneNumber": [
  {
   "type": "home",
   "number": "212 555-1234"
  },
  {
   "type": "fax",
   "number": "646 555-4567"
  }
 ]
}

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 with pretty print
$ jsonfiddle sort -i test/Customer.json
> test/CustomerSI.ref
{
 "address": {
  "city": "New York",
  "postalCode": "10021",
  "state": "NY",
  "streetAddress": "21 2nd Street"
 },
 "age": 25,
 "firstName": "John",
 "lastName": "Smith",
 "phoneNumber": [
  {
   "number": "212 555-1234",
   "type": "home"
  },
  {
   "number": "646 555-4567",
   "type": "fax"
  }
 ]
}
Sort in compact
$ jsonfiddle sort -c -i test/Customer.json
> test/CustomerSC.ref
{"address":{"city":"New York","postalCode":"10021","state":"NY","streetAddress":"21 2nd Street"},"age":25,"firstName":"John","lastName":"Smith","phoneNumber":[{"number":"212 555-1234","type":"home"},{"number":"646 555-4567","type":"fax"}]}

JSON to struct via jsonfiddle j2s

$ jsonfiddle j2s -i test/Customer.json
> test/CustomerJ2S.ref
package main

type Customer struct {
	Address struct {
		City          string `json:"city"`
		PostalCode    string `json:"postalCode"`
		State         string `json:"state"`
		StreetAddress string `json:"streetAddress"`
	} `json:"address"`
	Age         int64  `json:"age"`
	FirstName   string `json:"firstName"`
	LastName    string `json:"lastName"`
	PhoneNumber []struct {
		Number string `json:"number"`
		Type   string `json:"type"`
	} `json:"phoneNumber"`
}

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/jsonfiddle/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 by travis-ci.
  • Pick & choose the binary executable that suits your OS and its architecture. E.g., for Linux, it would most probably be the jsonfiddle_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., jsonfiddle, 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.

Author(s) & Contributor(s)

Tong SUN
suntong from cpan.org

All patches welcome.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL