Reputation: 121
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.
Upvotes: 1
Views: 136
Reputation: 7335
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
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,
Upvotes: 1