2

I am still learing python and I am trying to parse data from a JSON which looks like

{"297050": [[12, 137], [193, 776]], "297056": [[12, 203]]

but I cannot find a way to read it such as I get like

For entry 297050 this is the list [12, 137], [193, 776] For entry 297056 this is the list [12, 203]

I tried something like that

import json
from pprint import pprint

input_file = open ('file_JSON.txt')
json_array = json.load(input_file)
store_list = []


for obj in json_array :
    print obj,json_array[obj]

which gives me the obj and the array for each one

297050 [[12, 137], [193, 776]]

but I would like to actually be able to print each one of the elements that appear inside for instance the json_array[obj][0]...etc

Terma
  • 81
  • 8
  • 2
    Please provide expected result – Alderven May 17 '19 at 14:38
  • Because these are nested arrays, you won’t be able to get the values without doing a little more work. Have a look here: https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists – brandonscript May 17 '19 at 14:38

2 Answers2

1

You should probably use nested for loops.. what exactly you should print depends on how you expect your output to look.. but you could try this:

#!/usr/bin/env python3
import json

data = '{"297050": [[12, 137], [193, 776]], "297056": [[12, 203]]}'

data = json.loads(data)

for k, v in data.items():
    print(k)
    for list_of_ints in v:
        for integer in list_of_ints:
            print(integer)

Result:

297050
12
137
193
776
297056
12
203

Explanation:

We load in the sample json, and use items to iterate over it's key value pairs. Now our keys are in k and the outermost list is in v. v is a list, and a list of lists.. so we iterate that as list_of_ints. And finally we loop over each of those lists, printing out the innermost integers as we go.

If what you want as your output is this:

For entry 297050 this is the list [12, 137], [193, 776] For entry 297056 this is the list [12, 203]

Then we can just modify the script a little.. and get rid of a lot of the loops.

#!/usr/bin/env python3
import json

output = "For entry {} this is the list {}"
data = '{"297050": [[12, 137], [193, 776]], "297056": [[12, 203]]}'

data = json.loads(data)

for k, v in data.items():
    lists_with_commas = ", ".join([str(x) for x in v])
    print(output.format(k, lists_with_commas), end=" ")

Output

For entry 297050 this is the list [12, 137], [193, 776] For entry 297056 this is the list [12, 203]

Explanation:

We use a template string.. it has {} where we want to put stuff so we can run .format on it later.

We only need the keys and the innermost lists. So we only need the one for loop. We make sure we get the commas in the example by using .join and inside there we do a list comprehension, which turns all the lists into strings inside v.

Zhenhir
  • 1,157
  • 8
  • 13
0

You can do this. To get the values out of list in a linear way.

from itertools import chain

j = {"297050": [[12, 137], [193, 776]], "297056": [[12, 203]]}

for k in j:
     print(k)
     print('\t' + ', '.join(map(str, chain(*j[k]))))

Praveenkumar
  • 2,056
  • 1
  • 9
  • 18