Skip to content

kobylin/format-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Format JSON

Command line tool for formatting json.

Based on library json-stable-stringify

Installation

sudo npm install json-format-cli -g

Then use it as command line tool:

$ json-format foo.json

What it can?

  • Sorting properties alphabetically
  • Formatting json with specified indentation

Can be useful when response from server is not formatted or properties come in different order every time.

Options

--indent - number of chars used for indentation. 4 spaces by default
--indent-char - char used for indentation 'space' or 'tab'. space by default

You can pass any numbers of files to it

json-format foo.json bar.json

In this case every file will be overwritten by formatted file.

Using pipes

cat foo.json | json-format > result.json

curl https://api.chucknorris.io/jokes/random | json-format

Formatting example

//input
{"category":null,"icon_url":"https:\/\/assets.chucknorris.host\/img\/avatar\/chuck-norris.png","id":"zvno1ZJIQfetnTX5ye8TwQ","url":"https:\/\/api.chucknorris.io\/jokes\/zvno1ZJIQfetnTX5ye8TwQ","value":"The Old Spice Man aspires to smell like Chuck Norris"}

//output
{
    "category": null,
    "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
    "id": "zvno1ZJIQfetnTX5ye8TwQ",
    "url": "https://api.chucknorris.io/jokes/zvno1ZJIQfetnTX5ye8TwQ",
    "value": "The Old Spice Man aspires to smell like Chuck Norris"
}

Sorting props

//input
{
    "b": 10,
    "a": 1,
    "x": {
        "foo": 1,
        "bar": 2
    }
}

//output
{
    "a": 1,
    "b": 10,
    "x": {
        "bar": 2,
        "foo": 1
    }
}

About

Small shell utility for formatting JSON files

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published