0

I have a very basic problem that I can't figure out. I keep getting a "End of file expected.json" when trying to write object data to a json file. I was wondering how I can fix that? I do it by writing in a for loop. Not sure how I can format.

This is the code in question

with open("data.json", "w") as outfile:
    for x,y in structures.infrastructures.items():
        outfile.write(Sector(x, y["Depended on by"],y["Depends on"], y["Sub sections"]).toJson())

and this is the output

{
    "name": "Chemical",
    "depended_on": [
        "Critical Manufacturing",
        "Nuclear Reactors, Waste, and Material Management",
        "Commercial",
        "Healthcare and Public Health",
        "Food and Agriculture",
        "Energy"
    ],
    "depends_on": [
        "Emergency Services",
        "Energy",
        "Food and Agriculture",
        "Healthcare and Public Health",
        "Information Technology",
        "Nuclear Reactors, Waste, and Material Management",
        "Transportation Systems",
        "Water"
    ],
    "sub_sections": [
        "Chemical Plants",
        "Chemical Refineries",
        "Labs"
    ],
    "Status": 0,
    "Strain": 0
}{                                  -> this is where the error is
    "name": "Commercial",
    "depended_on": [
    ....
    ....
    etc

This is my toJson method:

    def toJson(self):
        return json.dumps(self, default=lambda o: o.__dict__, indent=4)

But yeah how can I implement it where my object data is written in JSON format?

2 Answers 2

2

A valid json file can only contain one object. Collect your data into a list and write it with a single call, or simulate the format with your code.

Sign up to request clarification or add additional context in comments.

Comments

-1

You have to use a function to parse the .json

I hope this helps

https://scriptcrunch.com/parse-json-file-using-python/#:~:text=How%20To%20Parse%20JSON%20File%20Content%20Using%20Python,also%20loop%20through%20all%20the%20JSON%20objects.%20

1 Comment

Parsing isn't the problem here. OP is writing invalid JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.