{"id":898,"date":"2022-02-01T02:10:49","date_gmt":"2022-02-01T07:10:49","guid":{"rendered":"https:\/\/www.brunerd.com\/blog\/?p=898"},"modified":"2022-02-01T02:10:51","modified_gmt":"2022-02-01T07:10:51","slug":"jpt-see-json-differently","status":"publish","type":"post","link":"https:\/\/www.brunerd.com\/blog\/2022\/02\/01\/jpt-see-json-differently\/","title":{"rendered":"jpt: see JSON differently"},"content":{"rendered":"\n<p>JSON is wonderfully efficient at encoding logical structure and hierarchy into a textual form, but sometimes our eyes and brains need some help making sense of that. Let&#8217;s see how jpt can help!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"the-shape-of-json\">The Shape of JSON<\/h4>\n\n\n\n<p>JSON uses a small and simple palette of brackets and curly braces, to express nest-able structures like objects and arrays. However once <em>inside<\/em> those structures it&#8217;s hard to visualize the lineage of the members.<\/p>\n\n\n\n<p>Using jpt however, one can get a better idea of what the shape of a JSON document is. <\/p>\n\n\n\n<p>For example <code>object-person.json<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"person\": {\n    \"name\": \"Lindsay Bassett\",\n    \"nicknames\": &#91;\n      \"Lindy\",\n      \"Linds\"\n    ],\n    \"heightInInches\": 66,\n    \"head\": {\n      \"hair\": {\n        \"color\": \"light blond\",\n        \"length\": \"short\",\n        \"style\": \"A-line\"\n      },\n      \"eyes\": \"green\"\n    },\n    \"friendly\":true\n  }\n}<\/code><\/pre>\n\n\n\n<p>Using <code>jpt -L<\/code> we can output what I am currently calling &#8220;JSONPath Object Literals&#8221;. With JPOL the entire path is spelled out for each item as well as it&#8217;s value expressed as a JSON object. You can also roundtrip JPOL right back into <code>jpt<\/code> and you&#8217;ll get the JSON again.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>% jpt -L object-person.json\n$={}\n$&#91;\"person\"]={}\n$&#91;\"person\"]&#91;\"name\"]=\"Lindsay Bassett\"\n$&#91;\"person\"]&#91;\"nicknames\"]=&#91;]\n$&#91;\"person\"]&#91;\"nicknames\"]&#91;0]=\"Lindy\"\n$&#91;\"person\"]&#91;\"nicknames\"]&#91;1]=\"Linds\"\n$&#91;\"person\"]&#91;\"heightInInches\"]=66\n$&#91;\"person\"]&#91;\"head\"]={}\n$&#91;\"person\"]&#91;\"head\"]&#91;\"hair\"]={}\n$&#91;\"person\"]&#91;\"head\"]&#91;\"hair\"]&#91;\"color\"]=\"light blond\"\n$&#91;\"person\"]&#91;\"head\"]&#91;\"hair\"]&#91;\"length\"]=\"short\"\n$&#91;\"person\"]&#91;\"head\"]&#91;\"hair\"]&#91;\"style\"]=\"A-line\"\n$&#91;\"person\"]&#91;\"head\"]&#91;\"eyes\"]=\"green\"\n$&#91;\"person\"]&#91;\"friendly\"]=true\n\n#roundtrip back into jpt (-i0 means zero indents\/single line)\njpt -L object-person.json | jpt -i0\n{\"person\":{\"name\":\"Lindsay Bassett\",\"nicknames\":&#91;\"Lindy\",\"Linds\"],\"heightInInches\":66,\"head\":{\"hair\":{\"color\":\"light blond\",\"length\":\"short\",\"style\":\"A-line\"},\"eyes\":\"green\"},\"friendly\":true}}\n<\/code><\/pre>\n\n\n\n<p>If you were to fire up <code>jsc<\/code> or some other JavaScript interpreter, declare <code>$<\/code> as a variable using <code>var $<\/code> and then copy and paste the output above and you will have this exact object.<\/p>\n\n\n\n<p>Perhaps though you prefer single quotes? Add -q to use single quotes for the property names but values remain double quoted.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#-q will single quote JSONPath only, strings remain double quoted \n\n% jpt -qL object-person.json         \n$={}\n$&#91;'person']={}\n$&#91;'person']&#91;'name']=\"Lindsay Bassett\"\n$&#91;'person']&#91;'nicknames']=&#91;]\n$&#91;'person']&#91;'nicknames']&#91;0]=\"Lindy\"\n$&#91;'person']&#91;'nicknames']&#91;1]=\"Linds\"\n$&#91;'person']&#91;'heightInInches']=66\n$&#91;'person']&#91;'head']={}\n$&#91;'person']&#91;'head']&#91;'hair']={}\n$&#91;'person']&#91;'head']&#91;'hair']&#91;'color']=\"light blond\"\n$&#91;'person']&#91;'head']&#91;'hair']&#91;'length']=\"short\"\n$&#91;'person']&#91;'head']&#91;'hair']&#91;'style']=\"A-line\"\n$&#91;'person']&#91;'head']&#91;'eyes']=\"green\"\n$&#91;'person']&#91;'friendly']=true\n\n#-Q will single quote both\n% jpt -QL object-person.json\n$={}\n$&#91;'person']={}\n$&#91;'person']&#91;'name']='Lindsay Bassett'\n$&#91;'person']&#91;'nicknames']=&#91;]\n$&#91;'person']&#91;'nicknames']&#91;0]='Lindy'\n$&#91;'person']&#91;'nicknames']&#91;1]='Linds'\n$&#91;'person']&#91;'height in inches']=66\n$&#91;'person']&#91;'head']={}\n$&#91;'person']&#91;'head']&#91;'hair']={}\n$&#91;'person']&#91;'head']&#91;'hair']&#91;'color']='light blond'\n$&#91;'person']&#91;'head']&#91;'hair']&#91;'length']='short'\n$&#91;'person']&#91;'head']&#91;'hair']&#91;'style']='A-line'\n$&#91;'person']&#91;'head']&#91;'eyes']='green'\n$&#91;'person']&#91;'friendly']=true\n<\/code><\/pre>\n\n\n\n<p>If you don&#8217;t like bracket notation use <code>-d<\/code> for dot notation (when the name permits). -Q will single quote both values and key names if using brackets. To save a few lines you can use -P to only print primitives (string, number, booleans, null) and will omit those empty object and array declarations. This too can roundtrip back into <code>jpt<\/code>, it will infer the structure type (object or array) based on the key type (string or number when in brackets).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>% jpt -dQLP object-person.json       \n$.person.name='Lindsay Bassett'\n$.person.nicknames&#91;0]='Lindy'\n$.person.nicknames&#91;1]='Linds'\n$.person.heightInInches=66\n$.person.head.hair.color='light blond'\n$.person.head.hair.length='short'\n$.person.head.hair.style='A-line'\n$.person.head.eyes='green'\n$.person.friendly=true\n\n#roundtrip into jpt, structure types can be inferred\n% jpt -dQLP object-person.json | jpt -i0    \n{\"person\":{\"name\":\"Lindsay Bassett\",\"nicknames\":&#91;\"Lindy\",\"Linds\"],\"heightInInches\":66,\"head\":{\"hair\":{\"color\":\"light blond\",\"length\":\"short\",\"style\":\"A-line\"},\"eyes\":\"green\"},\"friendly\":true}}\n<\/code><\/pre>\n\n\n\n<p>Now perhaps we don&#8217;t care so much about the data but about the structure? Then we can use the -J (JSONPath) or -R (JSON Pointer) output combined with -K or -k for the key names only<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#-K key names are double quoted\n% jpt -JK \/Users\/brunerd\/Documents\/TestFiles\/object-person.json\n$\n  \"person\"\n    \"name\"\n    \"nicknames\"\n      0\n      1\n    \"height in inches\"\n    \"head\"\n      \"hair\"\n        \"color\"\n        \"length\"\n        \"style\"\n      \"eyes\"\n    \"friendly\"\n\n#-q\/-Q can change the quoting to singles of course\n#-C will show the constructor type of every element\n% jpt -JKqC \/Users\/brunerd\/Documents\/TestFiles\/object-person.json\n$: Object\n  'person': Object\n    'name': String\n    'nicknames': Array\n      0: String\n      1: String\n    'height in inches': Number\n    'head': Object\n      'hair': Object\n        'color': String\n        'length': String\n        'style': String\n      'eyes': String\n    'friendly': Boolean\n\n\n#-k for unquotes key names, -C for constructor, -i1 for 1 space indents \n\n% jpt -JkC -i1 \/Users\/brunerd\/Documents\/TestFiles\/object-person.json\n$: Object\n person: Object\n  name: String\n  nicknames: Array\n   0: String\n   1: String\n  height in inches: Number\n  head: Object\n   hair: Object\n    color: String\n    length: String\n    style: String\n   eyes: String\n  friendly: Boolean\n<\/code><\/pre>\n\n\n\n<p>It&#8217;s been a lot of fun creating jpt and discovery new ways to express JSON using an emerging notation like JSONPath (which is going through the RFC process now). If you&#8217;d like to see JSON a different way <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/brunerd\/jpt\/releases\" target=\"_blank\">download jpt<\/a> and give it a try!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON is wonderfully efficient at encoding logical structure and hierarchy into a textual form, but sometimes our eyes and brains need some help making sense of that. Let&#8217;s see how jpt can help! The Shape of JSON JSON uses a small and simple palette of brackets and curly braces, to express nest-able structures like objects [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,51,41],"tags":[],"class_list":["post-898","post","type-post","status-publish","format-standard","hentry","category-jpt","category-json","category-jsonpath"],"_links":{"self":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/comments?post=898"}],"version-history":[{"count":8,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/898\/revisions"}],"predecessor-version":[{"id":1073,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/898\/revisions\/1073"}],"wp:attachment":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/media?parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/categories?post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/tags?post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}