Home > Mobile >  JSONPath filter not working on nested json that contains nested object instead of an array
JSONPath filter not working on nested json that contains nested object instead of an array

Time:04-13

I'm looking for a way to filter this JSON and retrieve only the cost when fid > 10

Working example, the parent prop is an array:

 [
    {
        "id": 1,
        "properties": {
            "parent": [
                {
                    "fid": 10,
                    "cost": 100
                }
            ]
        }
    },
    {
        "id": 2,
        "properties": {
            "parent": [
                {
                    "fid": 20,
                    "cost": 200
                }
            ]
        }
    },
    {
        "id": 32,
        "properties": {
            "parent": [
                {
                    "fid": 30,
                    "cost": 300
                }
            ]
        }
    }
]

JSONPath = $[*].properties.parent[?(@['fid']>10)].cost

result = [
  200,
  300
]

Not working example, the parent prop is now an object:

[
    {
        "id": 1,
        "properties": {
            "parent": {
                "fid": 10,
                "cost": 100
            }
        }
    },
    {
        "id": 2,
        "properties": {
            "parent": {
                "fid": 20,
                "cost": 200
            }
        }
    },
    {
        "id": 32,
        "properties": {
            "parent": {
                "fid": 30,
                "cost": 300
            }
        }
    }
]

JSONPath = $[*].properties.parent[?(@['fid']>10)].cost

The result is No match

CodePudding user response:

try this

$[?(@.properties.parent.fid>10)].properties.parent.cost
  • Related