-3
\$\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\$ – caird coinheringaahing Apr 2 at 14:08
  • \$\begingroup\$ thank you for your feedback! I've extended the question to include array and description \$\endgroup\$ – user109543 Apr 2 at 14:15
  • \$\begingroup\$ Another recommendation is to describe "JSON". Link to the JSON website, and put at least a partial description. \$\endgroup\$ – Wezl Apr 2 at 15:20
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, 11 bytes

-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\$
3
  • \$\begingroup\$ Wouldn't this be "Python 3 + -mjson.tool" and 0 bytes? \$\endgroup\$ – caird coinheringaahing Apr 2 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 Apr 2 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 Apr 2 at 14:20
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 Apr 2 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\$
2
  • 2
    \$\begingroup\$ 35 bytes \$\endgroup\$ – Arnauld Apr 2 at 14:51
  • \$\begingroup\$ @Arnauld Thanks. \$\endgroup\$ – iota Apr 2 at 15:10
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, privacy policy and cookie policy

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