-1

I have a string of multiple lists, each list contains multiple json objects

'[{"user": "use1", "fullname": "full1"}][{"user": "use2", "fullname": "full2"}, {"user": "use3", "fullname": "full3"}][{"user": "use4", "fullname": "full4"}]'

the expected result should be

[{"user": "use1", "fullname": "full1"},{"user": "use2", "fullname": "full2"},{"user": "use3", "fullname": "full3"},{"user": "use4", "fullname": "full4"}]

How can I get the json objects into one list?

leila
  • 461
  • 1
  • 7
  • 21
  • Possible duplicate of [multiple Json objects in one file extract by python](https://stackoverflow.com/questions/27907633/multiple-json-objects-in-one-file-extract-by-python) – Shuvojit Apr 16 '19 at 10:24
  • the answers there did not help y problem – leila Apr 16 '19 at 10:31
  • provide proper example and expected output. – Sociopath Apr 16 '19 at 10:37
  • done, you can check it out – leila Apr 16 '19 at 10:46
  • It seems there are some commas missing in your output. Also, is it intentionally that the parts with "use2" and "use3" are inside one square-bracket-block in your input? If i see it right, this may change a lot... –  Apr 16 '19 at 11:11
  • for the output i forgot the commas but yes, user2 and 3 are in a separate list – leila Apr 16 '19 at 11:25

1 Answers1

0
            data = data.split('[')[1:]

            data=[line[:-1] for line in data]

            elts = []
            for i in range(len(data)):
                try:
                    elt = json.loads(data[i], encoding='utf-8')
                    elts.append(elt)
                except Exception as e:
                    line= '[' + data[i] + ']'
                    elt = json.loads(line, encoding='utf-8')
                    elts.extend(elt)
leila
  • 461
  • 1
  • 7
  • 21