Pali
Pali

Reputation: 1357

JSONPath - get all values where object othervalue equals string

After several hours of reading and trying all the JSON Path expressions that came into my mind, both logic expressions and non-sense expressions, I still have no idea how I can extract all cmis:objectId where cmis:objectTypeId equals F:cm:custom from ALL object objects no matter the nested depth:

{

    {...   [... nested objects and arrays as needed for a tree strucutre
    object : {
        "succinctProperties": {

            "cmis:objectTypeId": "F:cm:custom",

            "cmis:objectId": "39cdd896-4563-4302-bba9-398006572522",
            ...
        }
    },
    }...   }... close nested objects and arrays as needed for a tree strucutre
    "id": "e244881e-e96b-406b-8d1f-faecae35d7f2"

}

Some things I tried and saved from my hundred attempts:

$.[*]..succinctProperties[?(@['cmis:objectTypeId']=='F:wim:caseEntries')].cmis:objectId


$.[*]..succinctProperties.cmis:objectId // Returns ALL without condition


$.[*].*..succinctProperties[?(@.['cmis:objectTypeId']=='F:wim:caseEntries')]

$.[*]..succinctProperties[@.cmis:objectTypeId=='F:wim:caseEntries')].cmis:objectId

Note: I am using http://www.jsonquerytool.com/ because I use the JMeter Plugin "JSON Path Extractor" and this plugin uses http://goessner.net/articles/JsonPath/

Upvotes: 2

Views: 6869

Answers (1)

Duncan
Duncan

Reputation: 841

I am not sure from your question whether you need cmis:objectTypeId to also always be directly under a succinctProperties object or whether it only needs to be somewhere under an object object. If the latter I believe I have a solution to your question. For the former I believe you may have reached the limits of what JSON Path is capable of.

I have used this sample JSON to test the query:

{
    "anotherobject": {
        "object" : {
            "someothernesting": {
                "succinctProperties": {
                    "cmis:objectTypeId": "F:cm:custom",
                    "cmis:objectId": "39cdd896-4563-4302-bba9-398006572522"
                }
            }
        }
    },
    "object" : {
        "succinctProperties": {
            "cmis:objectTypeId": "F:cm:custom",
            "cmis:objectId": "11111111-4563-4302-bba9-222222222222"
        }
    },
    "noobject": {
        "succinctProperties": {
            "cmis:objectTypeId": "F:cm:custom",
            "cmis:objectId": "3333333-4563-4302-bba9-4444444444"
        }
    }
}

This is the query. With $..object.. it looks for all objects anywhere under object objects, and then filters them by only those with a cmis:objectTypeId property:

$..object..[?(@['cmis:objectTypeId']=="F:cm:custom")].cmis:objectId

And these are the results (I also used http://www.jsonquerytool.com to test it):

[
    "11111111-4563-4302-bba9-222222222222",
    "39cdd896-4563-4302-bba9-398006572522"
]

The reason why something like $..succinctProperties[?(@['cmis:objectTypeId']=="F:cm:custom")] returns nothing is because JSON Path generally expects the entity being filtered (in this case succinctProperties) to be an array of objects. The @ refers to an object in the array. Since succinctProperties isn't an array, it treats the object's individual properties as parts of an array and therefore looks for the cmis:objectTypeId property on each property of succinctProperties and not on succinctProperties itself. It would therefore only match something if you had a structure like this:

{
    "succinctProperties": {
        "property": {
            "cmis:objectTypeId": "F:cm:custom",
            "cmis:objectId": "3333333-4563-4302-bba9-4444444444"
        }
    }
}

Upvotes: 6

Related Questions