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
9,517 views

2 answers to this question.

0 votes
import json
from pprint import pprint

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

pprint(data)

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

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

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.

https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/
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
782 views
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 in Python by Nymeria
• 3,520 points
613 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
86 views
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 in Python by Nymeria
• 3,520 points
82 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 332 views
+4 votes
6 answers
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
61 views
+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
7,359 views