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,780 points
1,994 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
• 58,020 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,220 points
1,079 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 2, 2020 in Python by Niroj
• 82,840 points
5,870 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, 2020 in Python by Rajiv
• 8,870 points
8,937 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,560 points
8,310 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
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
• 58,020 points
5,541 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
• 58,020 points
607 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP