1

How to get each field of json doc in XQUERY Marklogic?

let $doc :=
{
"field1" :'t',
"field2" : 'th',
"filed4": 'the'

}

return
$doc//??,
{
"New Filed" : "Added"
}

So how can we get the output like below ?

{ "field1" :'t', "field2" : 'th', "filed4": 'the' ,"New Filed" : "Added"}
grtjn
  • 20,254
  • 1
  • 24
  • 35
KumarG
  • 61
  • 1

2 Answers2

1

One approach: use the xdmp:from-json() to convert the immutable JSON node to a mutable map and then set the field:

return xdmp:from-json($doc) => map:with("NewField", "Added")

For more detail, see: https://docs.marklogic.com/xdmp:from-json

Hoping that helps,

ehennum
  • 7,295
  • 13
  • 9
0

A JSON object is really just a specialized map. So you can use map operators, like the + union operator:

let $doc := object-node
{
"field1" :'t',
"field2" : 'th',
"filed4": 'the'
}

return 
  $doc + map:entry("NewField", "added")
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147