-5

I am loading json from files using the code:

file = 'file_name'
obj_list = []
with open(file) as f:
    for json_obj in f:
        obj_list.append(loads(json_obj))

I get error:

JSONDecodeError: Extra data: line 1 column 21 (char 20)

All my files look like this but much larger.

{"some":"property2"}{"some":"property"}{"some":"property3"}

Is there a way to parse this in python for a large number of files?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
T-Jay
  • 347
  • 1
  • 4
  • 16
  • 2
    The example you posted isn't valid JSON. If they all look exactly like that (i.e., just one key/value, no nesting) you could probably write a custom thing to split them and then parse them separately as JSON. – BrenBarn Feb 02 '22 at 22:16
  • Its not a JSON file. – rv.kvetch Feb 02 '22 at 22:25

2 Answers2

2

Your json is not valid . It should be something like this

[{'some': 'property2'}, {'some': 'property'}, {'some': 'property3'}]

Nabil
  • 1,130
  • 5
  • 11
-2
import json
with open(file, 'r') as f:
    json_str = f'[{f.read()}]'
    obj_list = json.loads(json_str)

Reading the content, adding [] to make it valid json, and then loading it with the json package.

elgehelge
  • 2,014
  • 1
  • 19
  • 24