How to Parse values from a JSON file?

0 votes

I have this JSON in a file:

{
    "maps": [
        {
            "id": "helloworld",
            "iscategorical": "0"
        },
        {
            "id": "helloworld",
            "iscategorical": "0"
        }
    ],
    "masks": [
        "id": "valuerr"
    ],
    "om_points": "value",
    "parameters": [
        "id": "valuerr"
    ]
}

I wrote this script which prints all of the json text:

json_data=open(file_directory).read()

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

How can I parse the file and extract single values?

Oct 15, 2018 in Python by findingbugs
• 4,750 points
59 views

1 answer to this question.

0 votes
import json
from pprint import pprint

with open('data.json') as f:
    data = json.load(f)

pprint(data)

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

data["maps"][0]["id"]
data["masks"]["id"]
data["om_points"]
answered Oct 15, 2018 by Priyaj
• 56,900 points

Related Questions In Python

0 votes
1 answer

How to traverse from a file to parse another file?

You use os.walk() module of python for ...READ MORE

answered Jan 2 in Python by Omkar
• 67,600 points
37 views
0 votes
1 answer

How to use read a WSDL file from the file system using Python suds?

Hi, good question. It is a very simple ...READ MORE

answered Jan 21 in Python by Nymeria
• 3,520 points
580 views
0 votes
1 answer

How to create and read from a temporary file in Python?

Hi, there is a very simple solution ...READ MORE

answered Jan 29 in Python by Nymeria
• 3,520 points
80 views
0 votes
1 answer

How to extract values from a string or a sequence?

you could use the statements like name[index value ...READ MORE

answered Mar 6 in Python by Waseem
• 4,480 points
28 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 282 views
0 votes
1 answer

how to read a JSON from a file?

You can use with statement with open('strings.json') as ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 56,900 points
77 views
0 votes
1 answer

How do you append to a file?

with open("test.txt", "a") as myfile: myfile.write("appended text" ...READ MORE

answered Jul 27, 2018 in Python by Priyaj
• 56,900 points
30 views