Parsing comma separated JSON from file

I am reading a file that contains comma separated JSON. For example,

{
  ...JSON
},
{
  ...JSON
},
{
  ...JSON
}

      

I know for sure that they are separated by commas, but not sure if they are separated by newlines. This JSON could potentially be on the same line. But for sure they are separated by commas. I have not received this data yet. I am wondering how I can parse each JSON object and add it to the list.

pseudocode:

def source_parse(source_file):
   json_list = []
   with open(source_file) as source:
      json_source = source.readlines()
      # parse json_source
      json_list.append(json_obj)

      

0


source to share


2 answers


This is invalid JSON, it has no parentheses [...]

to make it a list.

You can add them manually:



with open(source_file) as source:
    json_source = source.read()
    data = json.loads('[{}]'.format(json_source))

      

+5


source


you can parse data into dict by following this link. Parsing values ​​from JSON file using Python?

and convert the dict to a list with

.items()

      



for the analyzed dictionary.

Hope this solves the problem. Good luck.

+1


source







All Articles