25

I have this JSON code:

{
    "A": {
        "AB": [{
            "ABA": "0",
            "ABB": "1",
            "ABC": "2"
        }]
    }
}

I need to use a JSONPath expression that returns that JSON with only ABA and ABC attributes. Something like:

{
    "A": {
        "AB": [{
            "ABA": "0",
            "ABC": "2"
        }]
    }
}

So far I manage to extract either one or all attributes. For example

$.A.AB[*]

or

$.A.AB[*].ABA

Is there a way to extract only two?

Thanks

galcyurio
  • 1,776
  • 15
  • 25
Spaffo
  • 621
  • 2
  • 8
  • 12

1 Answers1

49

This will work using the Jayway implementation (Java):

$.A.AB[*]['ABB', 'ABA']

and the result for your input would be:

[
   {
      "ABB" : "1",
      "ABA" : "0"
   }
]

You can Compare different providers here:

http://jsonpath.herokuapp.com/

xpt
  • 20,363
  • 37
  • 127
  • 216
kalle
  • 1,869
  • 14
  • 14
  • Thanks! I'm using the JSON Utils plugin with Jetbrains (Webstorm) and your pattern worked nicely to retrieve a couple of values from a lottie file layers[?(@.nm=='ang_eyebr_r')]['ip', 'op'] – Andy Dent Feb 18 '19 at 09:34
  • 1
    The Jayway JsonPath feature to extract a subset of members from a json object is actually a bug in the Jayway JsonPath parser, see https://github.com/json-path/JsonPath/issues/395. – Daniel Dec 11 '19 at 10:43
  • Just a quick note here, but on clients such as Insomnia, you can have spaces between the keys. – securisec Jun 04 '20 at 15:20
  • I believe @securisec means you **can't** have spaces between the keys in Insomnia. – William Denman Jun 15 '21 at 04:52
  • 1
    In Insomnia you can't have spaces or quotes. So the above would be `$.A.AB[*][ABB,ABA]` – Sarke Apr 04 '22 at 22:37
  • Thanks everyone for the Insomnia comments which solved my issue that was driving me crazy. – daybreaker Jun 21 '23 at 14:08