Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split json data python

I try to split json data using data[1:], but don’t work.
more concretely what I’m trying to do is ignore the first object array of a .json, but keep the rest in a variable

Py

f = open("people_data.json", "r")
data = json.load(f)

people = data[1:]

# irrelevant code 

JSON

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "variable_id": [
        {
            "name": "anybody",
            "age": "25",
            "job": "insurance company"
        }
    ],
    "Skills": [
        {
            "area": "programmer",
            "level": "junior",
            "comments": "..."
        }
    ],
    "other": [
        {
            "something": "..."
        }
    ]

My goal is to have a variable with skills and others, but without personal data.
This is just an example, because the problem is that I don’t know what will be the name of the following arrays because they will be variables instead of skills or other names

>Solution :

Your data is a dict, not a list, so slicing isn’t available. (There’s also the philosophical issue of whether you can assume that variable_id is the first item: Python dicts are ordered by insertion, but JSON objects are not.)

The simplest thing to do would be to simply remove the key you don’t want.

with open("people_data.json") as f:
    data = json.load(f)

people = dict(data)
people.pop("variable_id")

# Or just data.pop("variable_id") if you don't care about preserving the original dict.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading