Get keys in JSON

后端 未结 1 1823
心在旅途
心在旅途 2021-02-13 09:59

I get the following JSON result from an external system:

{
  \"key1\": \"val1\",
  \"key2\": \"val2\",
  \"key3\": \"val3\"
}

Now I want to dis

相关标签:
1条回答
  • 2021-02-13 10:50

    I found that the tilda ~ symbol is able to retrieve the keys of the values it's called upon. So for your example a query like this:

    $.*~
    

    Returns this:

    [
      "key1",
      "key2",
      "key3"
    ]
    

    Another example, if we had a JSON document like this:

      {
      "key1": "val1",
      "key2": "val2",
      "key3": {
          "key31":"val31",
          "key32":"val32"
      }
    }
    

    A query like this:

    $.key3.*~
    

    Would return this:

    [
      "key31",
      "key32"
    ]
    

    It's important to note that these examples work on JSONPath.com and some other simulators/online tools, but on some they don't. It might come from the fact that I found out about the tilda(~) operator in the JSONPath plus documentation and not the official one.

    0 讨论(0)
提交回复
热议问题