Using JSON Path to select which attributes to be returned from the API

Viewed 68

I'm trying to make something like an attribute selection mechanism.

what I'm trying to make is to send query prams to select with attributes to be returned from the API.

for example

JSON

{
"id": 10,
"name": "test1",
"featues": [
    {
        "id": "1",
        "name": "age",
        "value": {
            "type": "num",
            "value": 12
        }
    },
    {
        "id": "2",
        "name": "age",
        "value": {
            "type": "num",
            "value": 12
        }
    }
]

}

URL

url/resource?fields=$.featues[*].id

Current result

[
  "1",
  "2"
]

it extracted the value, I don't want that behavior, I want to keep the path to the value

what the behavior I want is

{
    "featues": [
        {
            "id": "1"
        },
        {
            "id": "2"
        }
    ]
}

any suggestions?

1 Answers

You can use this solution:


url/resource?fields=$.featues[*].['id', '']


// output:
[
    {
        "id": "1"
    },
    {
        "id": "2"
    }
]
Related