Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
3  
Import["out.json", {"Data", "x"}] ? –  cormullion Jul 30 '13 at 15:14
    
That works! Why? Care to expand this into a full answer? –  ford Jul 30 '13 at 16:10
    
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. –  cormullion Jul 30 '13 at 16:12

1 Answer 1

up vote 7 down vote accepted

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"])
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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