1

I am new to json so need a little help I need to get json value of each property, so when I ran the below code, I get the json in text.

let $doc := 
    for $sResult in ($result/search:result)
        let $uri := fn:data($sResult/@uri)
        let $docElements := $sResult/search:extracted 
        return xdmp:from-json-string($docElements/text())
return $doc

Output in text format:

[{"title":"xxxxxxxxx"}, {"species":"Animals"}]
[{"title":"yyyyyyyyy"}, {"species":"Animals"}]

So I want to get something like $docElements/title/text(), so that I get the value of title json property to do some string operations and then put them back in json object to get the desired output. So, not sure how to do the same.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147

1 Answers1

1

To modify the content of documents, consider using search:resolve-nodes() instead of search:search().

If the requirement is to get and modify JSON content before returning to a client, one approach is to

  1. convert each JSON node to a mutable map
  2. navigate and modify the map with map:get() and map:put()
  3. convert the modified map back to a JSON node

A sketch of this approach:

let $modifiedDocs :=
    for $inputDoc in search:resolve-nodes(...query..)
    let $tempMap := xdmp:from-json($inputDoc)
    let $_ := ... navigate and modify the map ...
    return xdmp:to-json($tempMap)

For completeness, if the goal were instead the modify the persisted JSON documents, the iteration might resemble the following sketch:

 for $oldDoc in search:resolve-nodes(...query..)
 let $tempMap := xdmp:from-json($inputDoc)
 let $_ := ... navigate and modify the map ...
 let $newDoc := xdmp:to-json($tempMap)
 return xdmp:node-replace($oldDoc, $newDoc)

Hoping that helps,

ehennum
  • 7,295
  • 13
  • 9
  • Thanks for the response, but I need to extract the values of each json property to do highlighting the search text.Also ,I dont need to change anything in the document, the string oertaions I have to perform on the text of json property and send to the UI layer, no changes to db documents. – Pragya Kapoor Nov 12 '20 at 09:55
  • Please suggest. – Pragya Kapoor Nov 17 '20 at 10:51