CAN
CAN

Reputation: 87

Json Xpath matching key with dynamic string

I got a json like:

    {
   "message":{
      "2.21.59.0.4":{
         "eventData":{
            "optionalParameters":{
               "sysconRead":"1129:3,286:0,287:0,4072:1,915:14",
               "data_2222":"https://company.com/executions/35314/35314314/jcat/testlogs/10.136.75.13/201105_135857/10.136.75.13_filedump.zip",
               "sysconRead":"RP100:2000,RP145:16,RP146:2,RP147:4,RP15:100,RP150:813,RP45:500,RP74:9171,RP750:1,RP807:15",
               "data_3333":"https://company.com/executions/35314/35314314/jcat/testlogs/10.136.75.14/201105_135855/10.136.75.14_filedump.zip"
            }
         }
      }
   }
}

In the optionalParameters there are keys like data_2222 that has random numbers after data_. I am trying to write an Xpath expression that takes all the key-value pairs that match data_ but fail to create one. If I take e.g. $.message.*.eventData.optionalParameters. it returns all key-values like:

[
  {
    "sysconRead": "RP100:2000,RP145:16,RP146:2,RP147:4,RP15:100,RP150:813,RP45:500,RP74:9171,RP750:1,RP807:15",
    "data_2222": "https://company.com/executions/35314/35314314/jcat/testlogs/10.136.75.13/201105_135857/10.136.75.13_filedump.zip",
    "data_3333": "https://company.com/executions/35314/35314314/jcat/testlogs/10.136.75.14/201105_135855/10.136.75.14_filedump.zip"
  }
]

I just want it to return :

[
  {
   "data_2222": "https://company.com/executions/35314/35314314/jcat/testlogs/10.136.75.13/201105_135857/10.136.75.13_filedump.zip",
    "data_3333": "https://company.com/executions/35314/35314314/jcat/testlogs/10.136.75.14/201105_135855/10.136.75.14_filedump.zip"
  }
]

Upvotes: 0

Views: 49

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

An XPath 3.1 expression to return the result you are looking for would be

[map:merge(?message?*?eventData?optionalParameters => 
   map:for-each(
     function($k,$v){if (matches($k, '^data_') then map{$k : $v} else ()}
   ))]

Upvotes: 1

Related Questions