Making sense of your JSON


Many time during my work, I see people struggling to read minified JSON or single line of JSON (of 800 chars...).

Usually we read better an indented text rather than single line, for example - which one is easier to read:

{"first":"foo","last":"bar","middle":"nothing","age":15,"address":{"address1":"address of someone","address2":"who doesn't want you to read this","city":"nowhere","country":"somewhere"},"agree":true}

Or this:

{
  "first": "foo",
  "last": "bar",
  "middle": "nothing",
  "age": 15,
  "address": {
    "address1": "address of someone",
    "address2": "who doesn't want you to read this",
    "city": "nowhere",
    "country": "somewhere"
  },
  "agree": true
}

hope you chose the second option <Hint/>

Several options you have to format:



  • Using terminal - use JQ

  • Using online tool - use JsonFormatter (when using online tool, make sure you don't expose personal info!)

Needless to say, you should try and search using your eyes, if you can simply search using a tool!

Hope this helped you
Making sense of your JSON Making sense of your JSON Reviewed by Ran Davidovitz on 2:14 PM Rating: 5

4 comments:

Anton S said...

Notepadd++ has Plugin “JSON Viewer”. This plugin has ability to format all text to Json format or only selected string.
Should be useful for WIndows users.

Anton S said...

Notepadd++ has Plugin “JSON Viewer”. This plugin has ability to format all text to Json format or only selected string.
Should be useful information for Windows users.

Ran Davidovitz said...

Anton, Thanks!

Yaron said...

A GNU approach to this issue:
grep -Eo '"[^"]*" *(: *([0-9]*|"[^"]*")[^{}\["]*|,)?|[^"\]\[\}\{]*|\{|\},?|\[|\],?|[0-9 ]*,?' | awk '{if ($0 ~ /^[}\]]/ ) offset-=4; printf "%*c%s\n", offset, " ", $0; if ($0 ~ /^[{\[]/) offset+=4}'

Ran suggested adding it as an alias in .bashrc for easier access.

Powered by Blogger.