Skip to content

A simple JavaScript program that formats JSON in a human-readable way. Uses a simple heuristic (no constraint optimization).

License

Notifications You must be signed in to change notification settings

garyg1/json-formatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Simple JSON Formatter

A simple JavaScript program that formats JSON in a human-readable way. Uses a heuristic approach (no constraint optimization).

JSON is a human- and machine-readable data representation language. However, humans and machines make different spacing and wrapping decisions when writing it. There are many times when developers in industry need to read large JSON objects (e.g. az vm list-skus -l uswest --all), and machine-formatted JSON is often not optimal for human readability.

For example, consider the following simple JSON definition.

[1, 2, 3, [4, 5, [6, 7, 8]], "9"]

The default VSCode JSON formatter will print

[
    1,
    2,
    3,
    [
        4,
        5,
        [
            6,
            7,
            8
        ]
    ],
    "9"
]

Humans write JSON in a more natural and readable way. For example:

[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
[
    1, 2, 3,
    [4, 5, [6, 7, 8]],
    "9"
]
[
    1, 2, 3,
    [
        4, 5,
        [6, 7, 8]
    ],
    "9"
]

Prior Art

Tl;dr - use prettier.

I did some quick Google searching (e.g. "json prettifier site:*.github.io") and looked at a few examples. I wasn't able to find one that fit the above use case. I decided it would take an hour to write my own. So that's what I did.

After writing this, I asked some friends what they use to format JSON:

How it works

This program uses a simple heuristic that is good enough in most cases: wrap the top-most elements (closest to the root) first until the subtree can fit on a single line.

The general problem is an optimization problem depending on the metric to optimize (# of lines, # of expansions, etc.). But this program's heuristic works well enough in practical cases.

Object Example

----------------------------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
--------------------------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
------------------------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
----------------------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
--------------------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
------------------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
----------------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
--------------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
------------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
----------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
--------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
------------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
----------------------------------------------------------------------------
{ "the quick brown fox": { "jumps": "over", "the lazy": ["d", "o", "g"] } }
--------------------------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
------------------------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
----------------------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
--------------------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
------------------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
----------------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
--------------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
------------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
----------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
--------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
------------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
----------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
--------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
------------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
----------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
--------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
------------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
----------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
--------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
------------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": ["d", "o", "g"]
    }
}
----------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d", "o", "g"
        ]
    }
}
--------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d", "o", "g"
        ]
    }
}
------------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d", "o", "g"
        ]
    }
}
----------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d", "o", "g"
        ]
    }
}
--------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d",
            "o",
            "g"
        ]
    }
}
------------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d",
            "o",
            "g"
        ]
    }
}
----------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d",
            "o",
            "g"
        ]
    }
}
--------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d",
            "o",
            "g"
        ]
    }
}
------------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d",
            "o",
            "g"
        ]
    }
}
----------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d",
            "o",
            "g"
        ]
    }
}
--------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d",
            "o",
            "g"
        ]
    }
}
------------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d",
            "o",
            "g"
        ]
    }
}
----------
{
    "the quick brown fox": {
        "jumps": "over",
        "the lazy": [
            "d",
            "o",
            "g"
        ]
    }
}

Array Example

------------------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
-----------------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
----------------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
---------------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
--------------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
-------------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
------------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
-----------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
----------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
---------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
--------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
-------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
------------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
-----------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
----------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
---------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
--------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
-------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
------------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
-----------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
----------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
---------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
--------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
-------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
------------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
-----------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
----------------------------------
[1, 2, 3, [4, 5, [6, 7, 8]], "9"]
---------------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
--------------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
-------------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
------------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
-----------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
----------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
---------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
--------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
-------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
------------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
-----------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
----------------------
[
    1,
    2,
    3,
    [4, 5, [6, 7, 8]],
    "9"
]
---------------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [6, 7, 8]
    ],
    "9"
]
--------------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [6, 7, 8]
    ],
    "9"
]
-------------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [6, 7, 8]
    ],
    "9"
]
------------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [6, 7, 8]
    ],
    "9"
]
-----------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [
            6,
            7,
            8
        ]
    ],
    "9"
]
----------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [
            6,
            7,
            8
        ]
    ],
    "9"
]
---------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [
            6,
            7,
            8
        ]
    ],
    "9"
]
--------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [
            6,
            7,
            8
        ]
    ],
    "9"
]
-------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [
            6,
            7,
            8
        ]
    ],
    "9"
]
------------
[
    1,
    2,
    3,
    [
        4,
        5,
        [
            6,
            7,
            8
        ]
    ],
    "9"
]
-----------
[
    1,
    2,
    3,
    [
        4,
        5,
        [
            6,
            7,
            8
        ]
    ],
    "9"
]
----------
[
    1,
    2,
    3,
    [
        4,
        5,
        [
            6,
            7,
            8
        ]
    ],
    "9"
]

About

A simple JavaScript program that formats JSON in a human-readable way. Uses a simple heuristic (no constraint optimization).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published