1

I want to write the XQuery to print the specific keys in JSON and want to except if it has an array value.

Sample JSON:

{
  "id":"743",
  "transation":{
    "101":"success",
    "102":"rejected",
    "301":"processing"
   },
  "groupName":"group1"
}

Expected Result:

id

groupName
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
Jayabalan
  • 361
  • 1
  • 7
  • You don't want to print the `transation` property. Just to clarify, it's an object - not an array. Is your requirement only to print if it's a simple property with an atomic value, and skip over objects AND arrays? – Mads Hansen Aug 22 '22 at 14:41
  • And are you reading the JSON as a document in the database? – Mads Hansen Aug 22 '22 at 14:52
  • Yes, reading JSON from the database, yes i need to skip objects and array – Jayabalan Aug 22 '22 at 15:55

2 Answers2

1

I'm not sure if marklogic follows the W3C spec here, but in XQuery 3.1 you can do

let $json := json-doc('my.json') 
return map:keys($json)[not($json(.) instance of array(*))]
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

Assuming that you are reading a JSON document from the database, you could iterate over the object property nodes and filter out the object/array properties by testing whether the property has child nodes:

for $property in  doc("/test.json")/object-node()/node()
where not($property/node())
return $property/name() 

Or you could exclude those that are instance of object-node() or array-node()

for $property in  doc("/test.json")/object-node()/node()
where not($property instance of object-node() or $property instance of array-node())
return $property/name() 
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147