PolarisUser
PolarisUser

Reputation: 739

Best way to parse sections of json in python3 to separate items in list

First off, I'm having trouble Googling this question since I don't know all of the terminology (so really, giving me the proper terms to use in my Google search would be just as useful in this question).

I have some JSON that I need to parse in python, put each JSON string in a list after its been parsed(List, not array for Python correct?) and then I am going to go through that list to push the JSON content back to my source.

So as of now, I can parse out a section of JSON that I want, but I am not sure how to then get down to just printing the section between brackets. For example, I want to get each section (brackets) in this block of code to be in a separate JSON line:

{
"components": [
    {
        "self": "MY URL",
        "id": "ID",
        "name": "NAME",
        "description": "THIS IS DESC",
        "isAssigneeTypeValid": false
    },
    {
        "self": "MY URL 2",
        "id": "ID",
        "name": "name",
        "isAssigneeTypeValid": false
    },
    {
        "self": "URL 3",
        "id": "ID",
        "name": "NAME 3",
        "description": "DESC",
        "isAssigneeTypeValid": false
    }
]
}

There is a lot more JSON in my file, but using this, I can get it down to just returning the text above.

datas = json.loads(data)
print(datas['components'])

So my question is how would I just print one block? Or access the first 'self' section?

Upvotes: 0

Views: 3988

Answers (3)

Ivana Balazevic
Ivana Balazevic

Reputation: 148

When you read it in, a JSON is a Python dictionary, so you can use all functions that are valid for dictionaries.

In your case "components" is the key of the dictionary, whose value is a list. Each item in the list is an another dictionary.

Upvotes: 1

AChampion
AChampion

Reputation: 30278

If you have a valid json document then you can simply iterate over the list of components. Assuming you name the file <id>.json then you can simply do:

datas = json.loads(data)
for component in datas['components']:
    with open("{}.json".format(component['id']), 'w') as f:
        json.dump(component, f)

Upvotes: 1

PM 2Ring
PM 2Ring

Reputation: 55489

Here's how you can iterate over that data, converting each dict in the "components" list back into JSON strings:

import json

data = '''
{
    "components": [
        {
            "self": "MY URL",
            "id": "ID",
            "name": "NAME",
            "description": "THIS IS DESC",
            "isAssigneeTypeValid": false
        },
        {
            "self": "MY URL 2",
            "id": "ID",
            "name": "name",
            "isAssigneeTypeValid": false
        },
        {
            "self": "URL 3",
            "id": "ID",
            "name": "NAME 3",
            "description": "DESC",
            "isAssigneeTypeValid": false
        }
    ]
}
'''

datas = json.loads(data)
for d in datas['components']:
    print(json.dumps(d))

output

{"self": "MY URL", "description": "THIS IS DESC", "id": "ID", "isAssigneeTypeValid": false, "name": "NAME"}
{"self": "MY URL 2", "id": "ID", "isAssigneeTypeValid": false, "name": "name"}
{"self": "URL 3", "description": "DESC", "id": "ID", "isAssigneeTypeValid": false, "name": "NAME 3"}

Upvotes: 3

Related Questions