Bahram
Bahram

Reputation: 1652

How to extract first element of an array from JSON via JsonPath expression

Consider the following JSON, I want to extract the first id (900731) by Jsonpath expression, I have tried expressions like $.content..id, $.content.*.*.id, $.content.*[0].id. they all return an array [900731,900732,900730], but I don't know why none of the below expressions works

$.content..id[0], $.content.*.*.id[0], $.content.*[0].id[0]

 {
    "content": {
        "67215d9d-8a31-4397-a5e7-e694026034be": [
            {
                "id": 900731
            }
        ],
        "e62d8fbf-86a7-4c59-afaa-469d16b90645": [
            {
                "id": 900732
            }
        ],
        "9ae6f7dd-177a-4895-9a90-aa7b75de11bd": [
            {
                "id": 900730
            }
        ]
    }
}

$.content['67215d9d-8a31-4397-a5e7-e694026034be'][0].id works, but it is not good, because the property name changes per API call

Any idea how to extract the first id? you can use https://jsonpath.com/ to try the expressions

Upvotes: 1

Views: 339

Answers (1)

Atbjbiye
Atbjbiye

Reputation: 36

None of the [$.content..id[0], $.content.*.*.id[0], $.content.*[0].id[0]] in your answer using the index 0 of the resulting array do not work because id is a dictionary.

The Python Dictionary object provides a key:value indexing facility. Dictionaries have been ordered since the Python 3.7 update, meaning you can use the integer keys, but you cannot access the values in a dictionary using an integer index. Since the values in the dictionary are indexed by keys, it is not the same as indexing a list or an array with an integer. In a list, string, or tuple, the integer index corresponds to the position of the element in the sequence. In a dictionary, the integer key is a label that maps to a value. Indexes can extract items in lists as said, and the numbers such as "67215d9d-8a31-4397-a5e7-e694026034be" is a list. This is why $.content.*[0].id was able to get the array.

If you are using python, you can check the work by printing the type of the variables. This is an example:

def print_id_from_json():

"""
Prints the id from a specific json dictionary
"""
    d = data['content']
    print(type(d)) #dict
    for number, value in d.items(): #number is "67215d9d-8a31-4397-a5e7-e694026034be"
        print(type(value)) #list
        for item in value:
            print(item['id']) 
            print(type(item)) #dict

print_id_from_json()

To print a list of the id's

def get_list_id_from_json():
    """
    Returns a list of id from a specific json dictionary
    """
    id_list = []
    d = data['content']
    for number, value in d.items(): #number is "67215d9d-8a31-4397-a5e7-e694026034be"
        for item in value:
            id_value = item['id']
            id_list.append(id_value)
    return id_list
    
print(get_list_id_from_json())

If your JSON looks like this:

{
    "content": {
        "67215d9d-8a31-4397-a5e7-e694026034be": [
            {
                "id": 900731
            }
        ],
        "e62d8fbf-86a7-4c59-afaa-469d16b90645": [
            {
                "id": 900732
            }
        ],
        "9ae6f7dd-177a-4895-9a90-aa7b75de11bd": [
            {
                "id": 900730
            }
        ]
    }
}



    

Upvotes: 1

Related Questions