Message Boards Message Boards

0
|
4888 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Using the results of imported JSON.

Posted 11 years ago

Hello, I feel that this is probably answered very clearly somewhere, but I have been unable to find the solution so far! I have samples of data in JSON format like this:-

[ {a: 1, b:1, c:2},{a:2, b:1, c:2}....so on]

I can import it with:-

data=Import["mydata.json"]

The data can then be addressed with a list index just fine, but I cannot see how to extract the a,b,c values into lists. I want to end up with something like:-

as={1,2}
bs={1,1}
cs={2,2}

Even just a hint as to the right documentation pages to look at would be great! Thanks for any help!

5 Replies

David,

Thanks!!

I had the same issue, today, and worked fine.

POSTED BY: Marcelo De Cicco

OK Thanks! Just starting out, so I think my question was more basic than it seemed. That's exactly what I needed to see! Probably trivial, but I really needed an example. Thanks very much!

Ok, I thought your question was the one I answered. Here are some examples of what I think you are asking based on my JSON file:

In[31]:= "required" /. jsonData

Out[31]= {"firstName", "lastName"}

In[33]:= "properties" /. jsonData

Out[33]= {"firstName" -> {"type" -> "string"}, 
 "lastName" -> {"type" -> "string"}, 
 "age" -> {"description" -> "Age in years", "minimum" -> 0, 
   "type" -> "integer"}}

In[34]:= "firstName" /. ("properties" /. jsonData)

Out[34]= {"type" -> "string"}

In[35]:= "type" /. ("firstName" /. ("properties" /. jsonData))

Out[35]= "string"
POSTED BY: David Reiss

That's very interesting, thanks! It is certainly useful to get all the LHS data. How could I get the RHS of a specific part of the JSON object? Like I just want an array of the values of the b part of my example? Thanks for your help!

I put the following JSON file on my computer in a file called test.json

{
    "title": "Example Schema",
    "type": "object",
    "properties": {
       "firstName": {
         "type": "string"
       },
       "lastName": {
         "type": "string"
       },
       "age": {
         "description": "Age in years",
         "type": "integer",
         "minimum": 0
       }
    },
    "required": ["firstName", "lastName"]
}

When I import it I get the following:

jsonData=Import["/Users/dreiss/Desktop/test.json"]

which gives the following:

{"required" -> {"firstName", "lastName"}, "title" -> "Example Schema",
  "type" -> "object", 
 "properties" -> {"firstName" -> {"type" -> "string"}, 
   "lastName" -> {"type" -> "string"}, 
   "age" -> {"description" -> "Age in years", "minimum" -> 0, 
     "type" -> "integer"}}}

Note that this is somewhat more nested than your example. If you want the left hand sides of the rules at the very top level just execute

First/@jsonData

which returns

{"required", "title", "type", "properties"}

Let's say you want all of the left hand sides of all rules then this would do the trick:

Cases[jsonData, (z_ -> _) :> z, Infinity]

which returns

{"required", "title", "type", "type", "firstName", "type", \
"lastName", "description", "minimum", "type", "age", "properties"}

By the way leaving out the Infinity, gives the same answer as the original computation which asks for just the first level:

Cases[jsonData, (z_ -> _) :> z]

returns

{"required", "title", "type", "properties"}

Let's say though though you want only the left hand sides all the way to only the third level

Cases[jsonData, (z_ -> _) :> z, 3]

gives

{"required", "title", "type", "firstName", "lastName", "age", \
"properties"}

And if you want it only at just the 3rd level you'd use this

Cases[jsonData, (z_ -> _) :> z, {3}]

which gives this:

{"firstName", "lastName", "age"}
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract