简体   繁体   中英

How to get a particular value from the given json

I have the following json

`{
    "count": 4,
    "node": [{
        "assetRecord": {
            "additionalhardware": null,
            "admin": null,
            "assetNumber": null,
            "autoenable": null,
            "building": null,
            "category": "Unspecified",
            "circuitId": null,
            "comment": null,
            "connection": null,
            "cpu": null,
            "dateInstalled": null,
            "department": null,
            "description": null,
            "displayCategory": null,
            "division": null,
            "enable": null,
            "floor": null,
            "hdd1": null,
            "hdd2": null,
            "hdd3": null,
            "hdd4": null,
            "hdd5": null,
            "hdd6": null,
            "id": 10817,
            "inputpower": null,
            "lastModifiedBy": "",
            "lastModifiedDate": 1491855810758,
            "lease": null,
            "leaseExpires": null,
            "maintContractExpiration": null,
            "maintContractNumber": null,
            "maintcontract": null,
            "managedObjectInstance": null,
            "managedObjectType": null,
            "manufacturer": null,
            "modelNumber": null,
            "notifyCategory": null,
            "numpowersupplies": null,
            "operatingSystem": null,
            "password": null,
            "pollerCategory": null,
            "port": null,
            "rack": null,
            "rackunitheight": null,
            "ram": null,
            "region": null,
            "room": null,
            "serialNumber": null,
            "slot": null,
            "snmpcommunity": null,
            "storagectrl": null,
            "supportPhone": null,
            "thresholdCategory": null,
            "username": null,
            "vendor": null,
            "vendorAssetNumber": null,
            "vendorFax": null,
            "vendorPhone": null,
            "vmwareManagedEntityType": null,
            "vmwareManagedObjectId": null,
            "vmwareManagementServer": null,
            "vmwareState": null,
            "vmwareTopologyInfo": null
        },
        "categories": [],
        "createTime": 1491855810758,
        "foreignId": null,
        "foreignSource": null,
        "id": "564",
        "label": "10.21.204.174",
        "labelSource": "A",
        "lastCapsdPoll": 1491855810984,
        "type": "A"
    }, {
        "assetRecord": {
            "additionalhardware": null,
            "admin": null,
            "assetNumber": null,
            "autoenable": null,
            "building": null,
            "category": "Unspecified",
            "circuitId": null,
            "comment": null,
            "connection": null,
            "cpu": null,
            "dateInstalled": null,
            "department": null,
            "description": null,
            "displayCategory": null,
            "division": null,
            "enable": null,
            "floor": null,
            "hdd1": null,
            "hdd2": null,
            "hdd3": null,
            "hdd4": null,
            "hdd5": null,
            "hdd6": null,
            "id": 10589,
            "inputpower": null,
            "lastModifiedBy": "",
            "lastModifiedDate": 1490819238331,
            "lease": null,
            "leaseExpires": null,
            "maintContractExpiration": null,
            "maintContractNumber": null,
            "maintcontract": null,
            "managedObjectInstance": null,
            "managedObjectType": null,
            "manufacturer": null,
            "modelNumber": null,
            "notifyCategory": null,
            "numpowersupplies": null,
            "operatingSystem": null,
            "password": null,
            "pollerCategory": null,
            "port": null,
            "rack": null,
            "rackunitheight": null,
            "ram": null,
            "region": null,
            "room": null,
            "serialNumber": null,
            "slot": null,
            "snmpcommunity": null,
            "storagectrl": null,
            "supportPhone": null,
            "thresholdCategory": null,
            "username": null,
            "vendor": null,
            "vendorAssetNumber": null,
            "vendorFax": null,
            "vendorPhone": null,
            "vmwareManagedEntityType": null,
            "vmwareManagedObjectId": null,
            "vmwareManagementServer": null,
            "vmwareState": null,
            "vmwareTopologyInfo": null
        },
        "categories": [],
        "createTime": 1490819238331,
        "foreignId": null,
        "foreignSource": null,
        "id": "561",
        "label": "10.21.205.148",
        "labelSource": "A",
        "lastCapsdPoll": 1490819238557,
        "type": "A"
    }, {
        "assetRecord": {
            "additionalhardware": null,
            "admin": null,
            "assetNumber": null,
            "autoenable": null,
            "building": null,
            "category": "Unspecified",
            "circuitId": null,
            "comment": null,
            "connection": null,
            "cpu": null,
            "dateInstalled": null,
            "department": null,
            "description": null,
            "displayCategory": null,
            "division": null,
            "enable": null,
            "floor": null,
            "hdd1": null,
            "hdd2": null,
            "hdd3": null,
            "hdd4": null,
            "hdd5": null,
            "hdd6": null,
            "id": 7554,
            "inputpower": null,
            "lastModifiedBy": "",
            "lastModifiedDate": 1477563144159,
            "lease": null,
            "leaseExpires": null,
            "maintContractExpiration": null,
            "maintContractNumber": null,
            "maintcontract": null,
            "managedObjectInstance": null,
            "managedObjectType": null,
            "manufacturer": null,
            "modelNumber": null,
            "notifyCategory": null,
            "numpowersupplies": null,
            "operatingSystem": null,
            "password": null,
            "pollerCategory": null,
            "port": null,
            "rack": null,
            "rackunitheight": null,
            "ram": null,
            "region": null,
            "room": null,
            "serialNumber": null,
            "slot": null,
            "snmpcommunity": null,
            "storagectrl": null,
            "supportPhone": null,
            "thresholdCategory": null,
            "username": null,
            "vendor": null,
            "vendorAssetNumber": null,
            "vendorFax": null,
            "vendorPhone": null,
            "vmwareManagedEntityType": null,
            "vmwareManagedObjectId": null,
            "vmwareManagementServer": null,
            "vmwareState": null,
            "vmwareTopologyInfo": null
        },
        "categories": [],
        "createTime": 1477563144159,
        "foreignId": null,
        "foreignSource": null,
        "id": "462",
        "label": "10.21.50.153",
        "labelSource": "A",
        "lastCapsdPoll": 1490722710498,
        "type": "A"
    }, {
        "assetRecord": {
            "additionalhardware": null,
            "admin": null,
            "assetNumber": null,
            "autoenable": null,
            "building": null,
            "category": "Unspecified",
            "circuitId": null,
            "comment": null,
            "connection": null,
            "cpu": null,
            "dateInstalled": null,
            "department": null,
            "description": null,
            "displayCategory": null,
            "division": null,
            "enable": null,
            "floor": null,
            "hdd1": null,
            "hdd2": null,
            "hdd3": null,
            "hdd4": null,
            "hdd5": null,
            "hdd6": null,
            "id": 5119,
            "inputpower": null,
            "lastModifiedBy": "",
            "lastModifiedDate": 1461419153008,
            "lease": null,
            "leaseExpires": null,
            "maintContractExpiration": null,
            "maintContractNumber": null,
            "maintcontract": null,
            "managedObjectInstance": null,
            "managedObjectType": null,
            "manufacturer": null,
            "modelNumber": null,
            "notifyCategory": null,
            "numpowersupplies": null,
            "operatingSystem": null,
            "password": null,
            "pollerCategory": null,
            "port": null,
            "rack": null,
            "rackunitheight": null,
            "ram": null,
            "region": null,
            "room": null,
            "serialNumber": null,
            "slot": null,
            "snmpcommunity": null,
            "storagectrl": null,
            "supportPhone": null,
            "thresholdCategory": null,
            "username": null,
            "vendor": null,
            "vendorAssetNumber": null,
            "vendorFax": null,
            "vendorPhone": null,
            "vmwareManagedEntityType": null,
            "vmwareManagedObjectId": null,
            "vmwareManagementServer": null,
            "vmwareState": null,
            "vmwareTopologyInfo": null
        },
        "categories": [],
        "createTime": 1461419153008,
        "foreignId": null,
        "foreignSource": null,
        "id": "389",
        "label": "dummyname_xv1",
        "labelSource": "A",
        "lastCapsdPoll": 1490722710502,
        "type": "A"
    }],
    "totalCount": 271
}

This above code is actually dumped in a file (say abc.txt) and it is a valid JSON format. I need to open the file and traverse to the "label" field. I need to write a code which should extract/print the "label" value only if it is a IP ie something in format of 0.0.0.0

How to do so ?

Try to use json module to parse json file and iterate node :

json.load()

Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table.

If you want to validate IP string is valid , you can use inet_aton () .

Convert an IPv4 address from dotted-quad string format (for example, '123.45.67.89') to 32-bit packed binary format, as a string four characters in length.

import json
import socket


for i in json.load(open('ooo.json'))["node"]:
    try:
        socket.inet_aton(i["label"])
        print(i["label"])
    except Exception as e:
        pass

Result:

10.21.204.174
10.21.205.148
10.21.50.153

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