简体   繁体   中英

How to print a specific item from within a JSON file into python

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.

You need to index into the staff list, eg:

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)

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"))

您可以列出所有用户名

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM