5
$\begingroup$

When you have a deeply nested hierarchical JSON structure, tools like JSON Editor Online help a lot to get an overview of the structured data at various levels. In this way you control the level-of-detail, i.e. the complexity of the hierarchical object with a collapsible/expandable widget.

When you import a nested JSON string in Mathematica you get a nested list of Rules. Has anyone constructed a visualization tool to expand/collapse nested list of rules in Mathematica. Could you please share it with other community members that are not so expert in Wolfram language or perhaps assist in building it.

Postscripts

  • Wolfram Language supports a variety of 'Views' that can be used to display information and 'OpenerView' seems to be the most appropriate control and the closest to the widget I described.

  • Notice that JSON Editor Online displays next to each object the number of items contained, this is what the opener object should display when it is closed. When it is opened, it should display all the items. If the items are not arrays, it should display them as key->value, if they are arrays then it should display an index and the number of items contained and so on.

  • I have included in the hyperlink a sufficiently complex, real case scenario, JSON string to demonstrate the use of JSON Editor tool.

$\endgroup$
  • $\begingroup$ Take a look at Dataset. It doesn't currently have the capabilities you ask form, but it is under development and seems to going that way. I, myself, look forward to having the kind of display you ask for as the output form for datasets. $\endgroup$ – m_goldberg Aug 5 '15 at 1:08
  • $\begingroup$ I have already started using Dataset, it is fantastic. You are right, functionality can get there, but it will be more like a nested table visualization similar to Altova XML Spy or in the javascript world I like TreeGrid (treegrid.com/Grid). But the tool I am discussing here is a more plain, straitjacket visualization for the JSON structure. Plus the fact that you can use it on nested list of rules without converting them first to associations. There is also the educational aspect of it and of course it is going to take some time to complete such enhancements for the Dataset. $\endgroup$ – Athanassios Aug 5 '15 at 7:01
4
$\begingroup$

Something like this is a good start. I tried to write it in a way to make it easy to expand.

ruleListQ[{r__Rule}] = True; (* JSON won't have RuleDelayed *)
ruleListQ[_] = False;

formatJSON[json_] :=
 Switch[json,
  _?ruleListQ, (* dictionary *)
  Column@Replace[json, HoldPattern[a_ -> b_] :> OpenerView[{a, formatJSON[b]}], {1}],

  _?(ArrayQ[#, _, AtomQ] &), (* AtomQ makes sure that only arrays of basic types are formatted like this, not arrays of dictionaries *)
  TableForm[json],

  _List, (* list of non-basic types, including ragged arrays *)
  Column[formatJSON /@ json, Frame -> All],

  _, (* anything else *)
  json
  ]

Example (taken form the documentation):

Import["http://en.wikipedia.org/w/api.php?action=query&prop=links&titles=Wolfram%20Language&continue=&format=json", "JSON"] // formatJSON

enter image description here

$\endgroup$
  • $\begingroup$ It is a great start, thank you very much for your contribution $\endgroup$ – Athanassios Aug 5 '15 at 9:39
  • $\begingroup$ @Athanassios Here's a slightly improved version with support for associations ("RawJSON" import format in 10.2), that won't force us to open each opener view one by one when the contents are simple ayway. $\endgroup$ – Szabolcs Aug 5 '15 at 9:57
  • $\begingroup$ That makes it even better. Another basic but necessary improvement I think will be to open the items included in array objects with indexes next to it, see JSON Editor Online, to avoid repetition of the same structure. Then the user may choose to open just one of them to view what it looks like. That of course naturally leads to counting items embedded ;-) $\endgroup$ – Athanassios Aug 5 '15 at 10:10
  • $\begingroup$ @Athanassios You can add more patterns in the Switch to try to detect these cases, and handle them specially. Just make sure that specific patterns come first and general ones come only later. Switch will stop looking as soon as a pattern matches. $\endgroup$ – Szabolcs Aug 5 '15 at 10:33
  • $\begingroup$ It's relatively easy to transform a JSON to any expression using this method, for as long as the transformation of a sub-part does not depend on a previous, separate subpart. You could adapt it to your own special json files. For example, if certain files have 2D point coordinates for polygons {"coords": ... }, one more line in the Switch` can detect that and format it as Graphics. $\endgroup$ – Szabolcs Aug 5 '15 at 10:39

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.