1

I have below JSON structure :

{
    "key" : "value",
    "array" : [
        { "key" : 1 },
        { "key" : 2, "misc": {
                "a": "Apple",
                "b": "Butterfly",
                "c": "Cat",
                "d": "Dog"
            } },
        { "key" : 3 }
    ],
    "tokenize" : {
         "firstkey" : {
                      "token" : 0
                    },
         "secondkey" : {
                      "token" : 1
                    },
         "thirdkey" : {
                      "token" : 0
                    }
      }

}

I am able to traverse the above structure till array->dictionary->b by the below syntax :

$.array[?(@.key=2)].misc.b

Now I need to print all the tokens which has value 0. The same way as shown above I can traverse till $.array[?(@.key=2)].tokenize.

How can I query it to print all values having token:0 .

To be very precise, I want the output to be shown as :

[

      "tokenize" : {
         "firstkey" : {
                      "token" : 0
                    },
         "thirdkey" : {
                          "token" : 0
                        }
          }
]

The following query already showing something near to what I want but it does not show the keys ("firstkey" and "thirdkey" in this case).

 $.tokenize[?(@.token == 0)]

Please help me to get this as well.

Thanks.

Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44

2 Answers2

2

You can try this script.

$.tokenize[?(@.token == 0)].token

Result:

[
    0,
    0
]
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44
  • @sarsalan I have edited my question to show how do I want the output to be shown. Your answer is just printing 0, I want the entire structue having token == 0 to be printed. Hope I could explain you the requirement. Thanks – A.G.Progm.Enthusiast Nov 18 '17 at 18:01
  • Basically I want all the keys(along with their corresponding values) of 'tokenize' to be printed for where token == 0. – A.G.Progm.Enthusiast Nov 19 '17 at 03:09
  • 1
    I know that JSonPath only pays attention to values of nodes. And it seems is very primitive language and doesn't have any extended params for keys. – Serkan Arslan Nov 19 '17 at 16:09
0
$.tokenize[?(@.token == 0)]~

will output

[
  "firstkey",
  "thirdkey"
]

for the OP's sample json, use https://jsonpath-plus.github.io/JSONPath/demo/ to verify against your data.

toasterpic
  • 11
  • 2