views:

265

answers:

3

File json:

{"maps":[{"id":"blabla","iscategorical":"0"},{"id":"blabla","iscategorical":"0"}],
"masks":["id":"valore"],
"om_points":"value",
"parameters":["id":"valore"]
}

I write this script but it only print all the text.

json_data=open(file_directory).read()

data = json.loads(json_data)
pprint(data)

How can I parse the file and extract single values?

Thanks in advance.

A: 

You really, really need to read this.

Ignacio Vazquez-Abrams
+1  A: 

I think what Ignacio is saying is that your json file is incorrect. You have []s when you should {}s. []s are for arrays, {}s are for dicts. Here's how your json file should look(your json file wouldn't even load for me):

{"maps":[{"id":"blabla","iscategorical":"0"},{"id":"blabla","iscategorical":"0"}],
"masks":{"id":"valore"},
"om_points":"value",
"parameters":{"id":"valore"}
}

Then you can use your code:

import json
from pprint import pprint
json_data=open('json_data')

data = json.load(json_data)
pprint(data)
json_data.close()

With data, you can now also find values in like so:

data["maps"][0]["id"]
data["masks"]["id"]
data["om_points"]

Try those out and see if it starts to make sense.

Justin Peel
Ok so I have to control my code because this json file is generated from a java object.Thanks.
michele
A: 

Ok I have resolved. Thanks at all.

michele