0
\$\begingroup\$

I want format a JSON string into human-readable form. A string like this:

'{"foo":"hello","bar":"world","c":[1,55,"bye"]}'

would be formatted as:

{
  "foo": "hello",
  "bar": "world",
  "c": [
    1,
    55,
    "bye"
  ]
}

Rules:

  1. For objects and arrays properties and items should be starting in new line with indent of +2 as you see in the above mentioned example.

  2. Space after colon is mandatory

  3. This is code-golf. Shortest code in bytes wins!

please be aware that json string should be part of the answer (but we will omit that from number of bytes, example:

echo '{"foo":"hello","bar":"world","c":[1,55,"bye"]}'|npx json

will be counted as 14 bytes => echo |npx json after omitting the string

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Welcome to Code Golf! For future reference, we recommend using the Sandbox to get feedback on challenge ideas before posting them to main. I've voted to close your challenge as unclear, as the specifications on how to "pretty print" aren't clear. For example, what if one of the objects is a list/dict, rather than being a string (e.g. {'a':{'b':'c', 'd':'e'}, 'f':[1,2,3]}) \$\endgroup\$ Commented Apr 2, 2021 at 14:08
  • \$\begingroup\$ thank you for your feedback! I've extended the question to include array and description \$\endgroup\$
    – user109543
    Commented Apr 2, 2021 at 14:15
  • \$\begingroup\$ Another recommendation is to describe "JSON". Link to the JSON website, and put at least a partial description. \$\endgroup\$
    – Wezl
    Commented Apr 2, 2021 at 15:20

5 Answers 5

3
\$\begingroup\$

APL (Dyalog Unicode), 18 bytes (SBCS)

Anonymous tacit prefix function.

⎕JSON⍠'Compact'0⍣2

Try it online!

⎕JSON is the built-in to/from JSON converter

sets options, here:

'Compact' is switched off (0)

⍣2 does the conversion twice (JSON string to APL array, from APL array to JSON string)

\$\endgroup\$
2
\$\begingroup\$

Python 3, 22 bytes

-mjson.tool --indent=2

Attempt This Online! Note that due to the way ATO works, the code is encoded in the options area and the input goes in the code area.

Previous 11-byte version predated the 2 space indent requirement by 4 seconds:

-mjson.tool

Try it online! Note that due to the way TIO works, the code goes in the command line options area and the input goes in the code area.

\$\endgroup\$
5
  • \$\begingroup\$ Wouldn't this be "Python 3 + -mjson.tool" and 0 bytes? \$\endgroup\$ Commented Apr 2, 2021 at 14:16
  • \$\begingroup\$ @ChartZBelatedly I don't think so, because python -mjson.tool isn't a programming language since it can only do one task \$\endgroup\$
    – pxeger
    Commented Apr 2, 2021 at 14:19
  • \$\begingroup\$ @ChartZBelatedly well, I guess the -m is the command-line flag here, and the json.tool is a builtin command? \$\endgroup\$
    – Neil
    Commented Apr 2, 2021 at 14:20
  • \$\begingroup\$ Doesn't this indent 4 spaces instead of the required 2? \$\endgroup\$
    – Adám
    Commented Apr 17 at 22:08
  • 1
    \$\begingroup\$ @Adám I checked and the 2 space requirement was edited in 4 seconds after I posted my answer, but thanks for letting me know; I've updated my answer accordingly. \$\endgroup\$
    – Neil
    Commented Apr 17 at 23:59
1
\$\begingroup\$

JavaScript, 24 bytes

o=>JSON.stringify(o,0,2)

Try it online!

\$\endgroup\$
1
  • 4
    \$\begingroup\$ I think the input is supposed to be a string. \$\endgroup\$
    – Arnauld
    Commented Apr 2, 2021 at 14:51
1
\$\begingroup\$

JavaScript, 39 35 bytes

s=>JSON.stringify(eval('_='+s),0,2)

Saved 4 bytes thanks to Arnauld.

f=
s=>JSON.stringify(eval('_='+s),0,2)

console.log(f('{"foo":"hello","bar":"world","c":[1,55,"bye"]}'))

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 35 bytes \$\endgroup\$
    – Arnauld
    Commented Apr 2, 2021 at 14:51
0
\$\begingroup\$

Java + Android, 41 bytes

s->new org.json.JSONObject(s).toString(2)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.