10
$\begingroup$

I'm having trouble importing JSON in the way I expect, based on my reading of the documentation. The particular issue I'm having is with importing a specific element, or list of elements. From the doc:

Import["file.json", "elem"] imports the specified element from a JSON file.

To test this, I'm trying the following:

In[1]:= Export["out.json", {"x"->42,"y"->17.5}]
Out[1]= out.json

The resulting file looks like:

{"x" : 42, 
"y" : 17.5}

Importing the entire file works, returning a list of rules:

In[2]:= Import["out.json"]
Out[2]= {x -> 42, y -> 17.5}

But, importing a specific element fails:

In[3]:= Import["out.json", "x"]
Import::noelem: The Import element "x" is not present when importing as JSON.
Out[3]= $Failed

What am I missing? Perhaps "element" doesn't mean what I think it means?

$\endgroup$
3
  • 3
    $\begingroup$ Import["out.json", {"Data", "x"}] ? $\endgroup$
    – cormullion
    Jul 30, 2013 at 15:14
  • $\begingroup$ That works! Why? Care to expand this into a full answer? $\endgroup$
    – ford
    Jul 30, 2013 at 16:10
  • $\begingroup$ To be honest, I don't know any more than "it usually works like that.". Perhaps someone else can write something more intelligent as an answer. $\endgroup$
    – cormullion
    Jul 30, 2013 at 16:12

1 Answer 1

11
$\begingroup$

Element as referred to in the documentation are not a JSON elements, but the kind of information you can get from the specified file format. For JSON the elements available is just Data as given by Import["out.json", "Elements"]. For a typical JPEG photo the available elements are:

{"Aperture", "BitDepth", "CameraTopOrientation", "ColorMap", \
"ColorProfileData", "ColorSpace", "Data", "Date", "Exif", "Exposure", \
"FocalLength", "Graphics", "GrayLevels", "Image", "ImageSize", \
"ImageWithExif", "ISOSpeed", "Manufacturer", "Model", "RawData", \
"RGBColorArray"}

Essentially, when you import a file to Mathematica what you get is a list of rules. One of the elements in this list, for a JPEG photo, is for example Aperture->None. You can inspect all the rules, the raw imported information if you like, by specifying "Rules" instead of "Elements" in Import. If we specify Import["photo.jpg","Aperture"] we're doing nothing but "Aperture"/.Import["photo.jpg","Rules"]. (This is probably not technically true, but it's a way of looking at it.)

As for JSON:

Import["out.json", "Rules"]

"Data" -> {"x" -> 42, "y" -> 17.5}

Import["out.json",{"Data","x"}] is what you're looking for (as Cormullion said). Data is an element of the JSON format, and "x" is referred to as the "subelement" of Data. In the documentation we can read more about the syntaxes that allows us to retrieve subelements:

elements

Returning to the list of rules we see that Import["out.json",{"Data","x"}] is equivalent to

"x" /. ("Data" /. Import["out.json", "Rules"])
$\endgroup$

Your Answer

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

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