0

How can I extract all the names from big JSON file using Python3.

with open('out.json', 'r') as f:
    data = f.read()

Here I'm opening JSON file after that I tried this

a = json.dumps(data)
b= json.loads(a)
print (b) 

Here is my data from JSON file.

{"data": [
{"errorCode":"E0000011","errorSummary":"Invalid token provided","errorLink":"E0000011","errorId":"oaeZ3PywqdMRWSQuA9_KML-ow","errorCauses":[]},
{"errorCode":"E0000011","errorSummary":"Invalid token provided","errorLink":"E0000011","errorId":"oaet_rFPO5bSkuEGKNI9a5vgQ","errorCauses":[]},
{"errorCode":"E0000011","errorSummary":"Invalid token provided","errorLink":"E0000011","errorId":"oaejsPt3fprRCOiYx-p7mbu5g","errorCauses":[]}]}

I need output like this

{"oaeZ3PywqdMRWSQuA9_KML-ow","oaet_rFPO5bSkuEGKNI9a5vgQ","oaejsPt3fprRCOiYx-p7mbu5g"}

I want all errorId.

  • Possible duplicate of [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – Azat Ibrakov Jun 24 '19 at 06:27

3 Answers3

2

Try like this :

n = {b['name'] for b in data['movie']['people']['actors']}
Vikas Periyadath
  • 3,088
  • 1
  • 21
  • 33
1

If you want to get or process the JSON data, you have to load the JSON first.

Here the example of the code

from json import loads
with open('out.json', 'r') as f:
    data = f.read()
    load = loads(data)

    names = [i['name'] for i in data['movie']['people']['actors']]

or you can change names = [i['name'] for i in data['movie']['people']['actors']] to Vikas P answers

1

Try using json module for the above.

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

actor_names = { names['name'] for names in data['movie']['people']['actors'] }
Shishir Naresh
  • 743
  • 4
  • 10