Home > OS >  JSON File multiple roots
JSON File multiple roots

Time:10-16

I have a JSON file I'm trying to manipulate in python but it seems the json formating is not correct:

{{"ticket":{"id":"123", "name":"bill"}}, {"ticket":{"id":"1234", "name":"james"}}}

When i try to format it using a json formatter it gives me Error multiple root elements

How can I fix it?

Update: with the suggestion from funqkey i updated the script:

import json
with open('ticketData8242020-6152021.json', 'r') as f:
     data = f.read()
     data = json.loads(data)

There is something wrong with the file. I will attempt to remove the ticket object references from the file. to fix it. Thanks everyone.

CodePudding user response:

The problems here include

  • ticket needs to be in quotes
  • When you have multiple objects, you need a list, not a dict
  • You can't have an object with multiple "ticket" keys.

I SUSPECT what you want is a list of objects, like this:

[{"id":"123", "name":"bill"}, {"id":"1234", "name":"james"}]

Or maybe a list of objects with one entry each, as funqkey suggested:

[{"ticket":{"id":"123", "name":"bill"}}, {"ticket":{"id":"1234", "name":"james"}}]

CodePudding user response:

# Should look like this [{"ticket": {"id": "123", "name": "bill"}}, {"ticket": {"id": "1234", "name": "james"}}]

import json

with open('ticketData8242020-6152021.json', 'r') as f:
    data = f.read()

data = json.loads(data)

CodePudding user response:

In JSON, the keys should be quoted using ". Therefore

{{ticket:{"id":"123", "name":"bill"}}, {ticket:{"id":"1234", "name":"james"}}}

is not a valid JSON. The corrected version is

{{"ticket":{"id":"123", "name":"bill"}}, {"ticket":{"id":"1234", "name":"james"}}}

You can validate your JSON online: JSON Online Validator and Formatter - JSON Lint

  • Related