user248176
user248176

Reputation: 101

open a .json file with multiple dictionaries

I have a problem that I can't solve with python, it is probably very stupid but I didn't manage to find the solution by myself.

I have a .json file where the results of a simulation are stored. The result is stored as a series of dictionaries like

{"F_t_in_max": 709.1800264942982, "F_t_out_max": 3333.1574129603068, "P_elec_max": 0.87088836042046958, "beta_max": 0.38091242406098391, "r0_max": 187.55175182942901, "r1_max": 1354.8636763521174, " speed ": 8}
{"F_t_in_max": 525.61428305710433, "F_t_out_max": 2965.0538075438467, "P_elec_max": 0.80977406754203796, "beta_max": 0.59471606595464666, "r0_max": 241.25371753877008, "r1_max": 688.61786996066826, " speed ": 9}
{"F_t_in_max": 453.71124051199763, "F_t_out_max": 2630.1763649193008, "P_elec_max": 0.64268078173342935, "beta_max": 1.0352896471221695, "r0_max": 249.32706230502498, "r1_max": 709.11415981343885, " speed ": 10}

I would like to open the file and and access the values like to plot "r0_max" as function of "speed" but I can't open unless there is only one dictionary. I use

with open('./results/rigid_wing_opt.json') as data_file:    
    data = json.load(data_file) 

but When the file contains more than one dictionary I get the error

ValueError: Extra data: line 5 column 1 - line 6 column 1 (char 217 - 431)

Upvotes: 10

Views: 18718

Answers (4)

aquavitae
aquavitae

Reputation: 19114

{...} {...} is not proper json. It is two json objects separated by a space. Unless you can change the format of the input file to correct this, I'd suggest you try something a little different. If the data is a simple as in your example, then you could do something like this:

with open('filename', 'r') as handle:
    text_data = handle.read()
    text_data = '[' + re.sub(r'\}\s\{', '},{', text_data) + ']'
    json_data = json.loads(text_data)

This should work even if your dictionaries are not on separate lines.

Upvotes: 3

Lix
Lix

Reputation: 47976

I would recommend reading the file line-by-line and convert each line independently to a dictionary.

You can place each line into a list with the following code:

import ast
# Read all lines into a list
with open(fname) as f:
    content = f.readlines()

# Convert each list item to a dict
content = [ ast.literal_eval( line ) for line in content ]

Or an even shorter version performing the list comprehension on the same line:

import ast
# Read all lines into a list
with open(fname) as f:
    content = [ ast.literal_eval( l ) for l in f.readlines() ] 

Upvotes: 3

Matthew Franglen
Matthew Franglen

Reputation: 4532

If your input data is exactly as provided then you should be able to interpret each individual dictionary using json.load. If each dictionary is on its own line then this should be sufficient:

with open('filename', 'r') as handle:
    json_data = [json.loads(line) for line in handle]

Upvotes: 12

Daniel Roseman
Daniel Roseman

Reputation: 599620

That is not valid JSON. You can't have multiple obje at the top level, without surrounding them by a list and inserting commas between them.

Upvotes: -2

Related Questions