1

I have a JSON object, and I'm trying to find the root elements under it. Can someone please help me to figure this out?

{
  "store" : {
      "10162021" : {
         "id" : 812340,
         "properties" : {
            "server" : "server1.example.org",
            "serverip" : "",
         }
      },
      "10162022" : {
         "properties" : {
            "serverip" : "127.0.0.1",
            "server" : "server2.example.org",
         },
         "id" : 859480
      }
   }
}

I need to extract the root elements 10162022, 10162021 based on the server name.

I have tried to use syntax like below, but it was not successful

$..*..[?(@.server == server2.example.org)]

I will appreciate any suggestions.

ikegami
  • 367,544
  • 15
  • 269
  • 518
rshdzrt
  • 125
  • 7
  • `$..*..[?(@.server == server2.example.org)]` is clearly not Perl. Is there a reason this is tagged Perl? – ikegami Feb 16 '21 at 18:52
  • @ikegami, yes this is not Perl but I want to implement this in Perl using jpath as I have been truing to execute this way ``` response_content.jpath.*..[?(@.server == server2.example.org)] ``` – rshdzrt Feb 16 '21 at 19:12
  • jpath is not dependent on the language of the program there uses the jpath library. Perl has no more relevance to the question than "Windows" or "Microsoft", and thus the tag has been removed. – ikegami Feb 16 '21 at 19:26

1 Answers1

1

It's not clear whether you want to return the keys "10162022", etc, or the values, like:

{
   "properties" : {
       "serverip" : "127.0.0.1",
       "server" : "server2.example.org"
   },
   "id" : 859480
}

If you want to return values, the following JSONPath should work:

$.store[?( @.properties.server=="server2.example.org" )]

If you want to return keys, I'm not entirely sure that's possible. JSONPath isn't really designed to find keys, but values.

If you need the keys, I would suggest pre-processing the structure to stash the keys into objects as values, like this:

{
  "store" : {
      "10162021" : {
         "__key" : "10162021",
         "id" : 812340,
         "properties" : {
            "server" : "server1.example.org",
            "serverip" : ""
         }
      },
      "10162022" : {
         "__key" : "10162022",
         "properties" : {
            "serverip" : "127.0.0.1",
            "server" : "server2.example.org"
         },
         "id" : 859480
      }
   }
}

Then use this JSONPath:

$.store[?( @.properties.server=="server2.example.org" )].__key
tobyink
  • 13,478
  • 1
  • 23
  • 35
  • thanks for the response I basically need to return the keys based on the server name. – rshdzrt Feb 16 '21 at 19:01
  • Also it's not possible to make changes to the JSON object as I'm consuming someone else's service. – rshdzrt Feb 16 '21 at 19:08
  • But you're writing some kind of script which takes their data and does stuff to it, right? So instead of your JSONPath query being the *first* thing you do, make it the *second* thing you do. And for the first thing you do, add the `__key` bits. – tobyink Feb 16 '21 at 19:20
  • This actually works, I will see if we can have the __key bits added, I was actually looking for something which actually works by default. I appreciate your response @tobyink – rshdzrt Feb 16 '21 at 19:45
  • You don't need to have `__key` added to the service that is providing the JSON; you just need to process the JSON between receiving it and querying it, so you can add the `__key` parts in yourself. – tobyink Feb 18 '21 at 10:09