1

I want to print a user from a JSON list into Python that I select however I can only print all the users. How do you print a specific user? At the moment I have this which prints all the users out in a ugly format

import json

with open('Admin_sample.json') as f:
    admin_json = json.load(f)

print(admin_json['staff'])

The JSON file looks like this

{
"staff": [
    {
        "id": "DA7153",
        "name": [
            "Fran\u00c3\u00a7ois",
            "Ullman"
        ],
        "department": {
            "name": "Admin"
        },
        "server_admin": "true"
    },
    {
        "id": "DA7356",
        "name": [
            "Bob",
            "Johnson"
        ],
        "department": {
            "name": "Admin"
        },
        "server_admin": "false"
    },
],
"assets": [
    {
        "asset_name": "ENGAGED SLOTH",
        "asset_type": "File",
        "owner": "DA8333",
        "details": {
            "security": {
                "cia": [
                    "HIGH",
                    "INTERMEDIATE",
                    "LOW"
                ],
                "data_categories": {
                    "Personal": "true",
                    "Personal Sensitive": "true",
                    "Customer Sensitive": "true"
                }
            },
            "retention": 2
        },
        "file_type": "Document",
        "server": {
            "server_name": "ISOLATED UGUISU",
            "ip": [
                10,
                234,
                148,
                52
            ]
        }
    },
    {
        "asset_name": "ISOLATED VIPER",
        "asset_type": "File",
        "owner": "DA8262",
        "details": {
            "security": {
                "cia": [
                    "LOW",
                    "HIGH",
                    "LOW"
                ],
                "data_categories": {
                    "Personal": "false",
                    "Personal Sensitive": "false",
                    "Customer Sensitive": "true"
                }
            },
            "retention": 2
        },
    },
]

I just can't work it out. Any help would be appreciated.

Thanks.

4 Answers 4

1

You need to index into the staff list, e.g.:

print(admin_json['staff'][0])

I suggest reading up a bit on dictionaries in Python. Dictionary values can be set to any object: in this case, the value of the staff key is set to a list of dicts. Here's an example that will loop through all the staff members and print their names:

staff_list = admin_json['staff']
for person in staff_list:
    name_parts = person['name']
    full_name = ' '.join(name_parts)  # combine name parts into a string
    print(full_name)
Sign up to request clarification or add additional context in comments.

Comments

0

Try something like this:

import json

def findStaffWithId(allStaff, id):
    for staff in allStaff:
        if staff["id"] == id:
            return staff
    return {}  # no staff found

with open('Admin_sample.json') as f:
    admin_json = json.load(f)

print(findStaffWithId(admin_json['staff'], "DA7356"))

Comments

0

You can list all the users name with

users = [user["name"] for user in admin_json['staff']]

Comments

0

You have two lists in this JSON file. When you try to parse it, you'll be reach a list. For example getting the first staff id:

print(admin_json['staff'][0]['id'])

This will print:

DA7153

When you use "json.loads" this will simply converts JSON file to the Python dictionary. For further info: https://docs.python.org/3/tutorial/datastructures.html#dictionaries

1 Comment

Welcome Zenka! Perhaps you could edit the original question with the correct JSON and remove it from your answer!

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.