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,790 points
520 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
• 57,640 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, 2019 in Python by Omkar
• 69,030 points
81 views
0 votes
1 answer

How to order_by a JSON from serializers.py file in django rest framework?

Hello @kartik, There's an easy way, just override ...READ MORE

answered Jul 1 in Python by Niroj
• 58,920 points
141 views
0 votes
1 answer

How to list a column of float values from csv file without importing (Python)

Using the default CSV module Demo: import csv with open(filename, "r") ...READ MORE

answered Sep 16 in Python by Rajiv
• 2,580 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, 2019 in Python by Nymeria
• 3,520 points
2,579 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, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,357 views
0 votes
0 answers
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
• 57,640 points
1,323 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
• 57,640 points
89 views