0

I am a Java developer trying to write a script in python. I have the following JSON: (json1)

{
    "name": "Data",
    "id": "9c27ea56-6f9e-4694-a088-c1423c114e88",
    "nodes": [
        {
            "id": "6eb8cb19-53e5-4f1d-88f3-119a3ee09b5d",
            "properties": [
                {
                    "visible": true,
                    "uniqueness": true,
                    "id": "ab6d8974-59a2-4f5d-b026-d9ed54886fa8",
                    "dataType": "String",
                    "shortName": "id",
                    "longName": "id"
                },
                {
                    "visible": true,
                    "uniqueness": false,
                    "id": "24f5547e-c360-4293-9e67-10b19d38c774",
                    "dataType": "String",
                    "shortName": "event_id",
                    "longName": "event_id"
                },
                {
                    "visible": true,
                    "uniqueness": false,
                    "id": "2929d02b-950f-4495-8b36-04ea4c142f8d",
                    "dataType": "String",
                    "shortName": "event_name",
                    "longName": "event_name"
                },
                {
                    "visible": true,
                    "uniqueness": false,
                    "id": "3456d02b-950f-4495-8b36-04ea4c142f8d",
                    "dataType": "String",
                    "shortName": "event_date",
                    "longName": "Event Date"
                }
            ]
        }
    ]
}

And I've another same json but some modifications. Something like this:(json2)

{
    "name": "Data",
    "id": "9c27ea56-6f9e-4694-a088-c1423c114e88",
    "nodes": [
        {
            "id": "6eb8cb19-53e5-4f1d-88f3-119a3ee09b5d",
            "properties": [
                {
                    "visible": true,
                    "uniqueness": true,
                    "id": "ab6d8974-59a2-4f5d-b026-d9ed54886fa8",
                    "dataType": "String",
                    "shortName": "id",
                    "longName": "id"
                },
                {
                    "visible": true,
                    "uniqueness": false,
                    "id": "24f5547e-c360-4293-9e67-10b19d38c774",
                    "dataType": "String",
                    "shortName": "event_id",
                    "longName": "event_id"
                },
                {
                    "visible": true,
                    "uniqueness": false,
                    "id": "2929d02b-950f-4495-8b36-04ea4c142f8d",
                    "dataType": "String",
                    "shortName": "event_name",
                    "longName": "Event Name"
                },
                {
                    "visible": true,
                    "uniqueness": false,
                    "id": "123245g-950f-4495-8b36-04ea4c142f8d",
                    "dataType": "String",
                    "shortName": "event_status",
                    "longName": "Event Status"
                }
            ]
        }
    ]
}

Now here in json2, I've added some more objects in the properties array other than json1.

When I'll iterate the json1 on the "properties" array I want to check whether it is present in the json2 array of "properties". And to check this I want to verify whether shortName is equal (in json1 properties array and json2 properties array)

Here json1 is considered to be as base object.

then How to filter "properties" array based on shortName (from "properties" array). As properties is an array of objects.

Harshit Thakurr
  • 195
  • 1
  • 4
  • 10
  • Does this answer your question? [Python: Find in list](https://stackoverflow.com/questions/9542738/python-find-in-list) – CrazyChucky Dec 12 '20 at 18:44
  • Specifically, that answer covers both getting a sublist (all items that match a certain criteria) or just grabbing the first item that matches, if you're only looking for one. And "arrays" in Python are called lists. `properties` (as interpreted by Python's usual JSON parsing) is a list of dictionaries. – CrazyChucky Dec 12 '20 at 18:46
  • DATA["nodes"][0]["properties"][0]['shortName'] this is how you can have access to property however you will have to itterate as per your need..i have used 0 for the first element only as demo – Sachin Rajput Dec 12 '20 at 18:47

1 Answers1

0

The most convenient way is to use json.loads, thus you could get then get the key, value pares.

# load the json object
jsonlist = json.loads(your_json)

#Show the list from properties 
print(jsonlist['properties'])

#get the kets from the json list 
for field in jsonlist['properties']:
    for key, value in field.items():
        print(key, value)