Extracting data from a JSON file in Python

0 votes
How do I parse a JSON file in python to read data from it?
Apr 19, 2018 in Python by noidea

2 answers to this question.

0 votes
import json
from pprint import pprint

data = json.load(open('data.json'))


This will store the JSON data in the dictionary named data. 

pprint() simply prints your data in a neat manner, preserving the indentation

Hope it helps!!

If you need to know more about Python, It's recommended to join Python course today.


answered Apr 19, 2018 by Nietzsche's daemon
• 4,260 points

I am new to geojson. Can someone please let me know how to extract co-ordinates from geojson file to python. i tried this and similar codes but received errors.

import geojson
with open("pvt.dat_191022_174643.geojson") as f:
    gj = geojson.load(f)
    print gj
for feature in gj:
  a= feature.properties.geometry.type 
  print a
  print feature.geometry.coordinates

Error: Traceback (most recent call last): File "read_geojson.py", line 8, in a= feature.properties.geometry.type AttributeError: 'str' object has no attribute 'properties'

Try replacing the 6th line with the following:

a = feature.geometry.type

Instead of  

a= feature.properties.geometry.type
0 votes
Here is what i found and was pretty useful too.

In this documentation you can find how to play with your json file.

answered Nov 27, 2018 by Rupali

Related Questions In Python

0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
0 votes
1 answer

Need help extracting a schema to make use for an avro file in Python

Hi, nice question. So what I daily use ...READ MORE

answered Jan 10, 2019 in Python by Nymeria
• 3,520 points
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, 2019 in Python by Nymeria
• 3,520 points
0 votes
1 answer

How can I read numbers in Python from a custom file?

Hi, good question. Let us first assume that ...READ MORE

answered Feb 6, 2019 in Python by Nymeria
• 3,520 points
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 2,402 views
0 votes
0 answers
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
0 votes
1 answer

Create and open a file in Python

file = open('text.txt', 'w+) READ MORE

answered May 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
+4 votes
7 answers

Splitting a List into chunks in Python

Here's a generator that yields the chunks ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points