0

I have a text file containing multiple valid json texts, something like this

{
    "items": [
        {
            "id": "someID",
            "vale": "someValue"
        },
        {
            "id": "otherID",
            "vale": "otherValue"
        }
    ]
}
{
    "items": [
        {
            "id": "yetAnotherID",
            "vale": "yetAnotherValue"
        },
        {
            "id": "lastID",
            "vale": "lastValue"
        }
    ]
}

but with many more items per json and many more json's.

My attempt to read it (not surprisingly) failed:

import os
import json
jsonFile = open(jsonName)
jsonList=json.load(jsonFile)

Any sugestions to get the concatenation of all lists in a python object?

Dirk Horsten
  • 3,753
  • 4
  • 20
  • 37
  • 1
    The first question is **why** you have this data. A few `,` and surrounding `[]` would transform it into valid JSON, after all. But if it really must be this way, please see the linked duplicate, particularly the answer mentioning `raw_decode`. – Karl Knechtel Feb 01 '23 at 20:28
  • I take it the tool outputs the JSON as a string on the standard output, then? – Karl Knechtel Feb 01 '23 at 20:39
  • Pardon; what "that" are you referring to? – Karl Knechtel Feb 01 '23 at 21:01
  • **I solved the problem** by replacing the end of a json followed by the start of one with a comma: `jsonText = jsonFile.read()` `jsonList=json.loads(re.sub('\n\s*\](\n.*)+?"items":\s*\[', ',', jsonText))` – Dirk Horsten Feb 01 '23 at 21:17

0 Answers0