1

I am struggling to write a JsonPath query to extract particular keys from the following sample Json.

{
    "initial": "somevalue",
    "somekey2": {
        "inner1": "innerval1",
        "inner2": "innerval2"
    }
}

For example:
1) I wish to extract the first key, which in this case is initial. Is this possible using JsonPath?

2) Get an inner key such as inner1. Something similar to $."initial"."somekey2" but returning an array with just the keys (inner1 and inner2).

Viktor
  • 2,623
  • 3
  • 19
  • 28
john
  • 1,561
  • 3
  • 20
  • 44

2 Answers2

3

This SO question covers it.

  1. $.*~ returns
[
  "initial",
  "somekey2"
]
  1. $.somekey2.*~ returns
[
  "inner1",
  "inner2"
]
  1. To get all 2nd order children use $.*.*~. Basically for nth order, $.(n times *).*~
Aakash Choubey
  • 426
  • 3
  • 19
-3

not sure about json path, but suppose your object is a

a={
    "initial": "somevalue",
    "somekey2": {
        "inner1": "innerval1",
        "inner2": "innerval2"
    }
};

the you can get all keys by using Object.keys(a) that will give array of keys ["initial", "somekey2"] then you can use that key to revtrive its nested value

a[Object.keys(a)[1]] // returns {inner1: "innerval1", inner2: "innerval2"}

and you can repeat the same for all nested element

Uttam Ughareja
  • 842
  • 2
  • 12
  • 21
  • 1
    This is using JS. Take a look at what JsonPath is here: http://goessner.net/articles/JsonPath/ – john Aug 31 '18 at 11:49