[英]Marklogic - Update within an array of element of a json using java api
Lets say we have json looks like 可以说我们有json看起来像
{
"name":"abc",
"lastName":"xyz",
"description":"aaaaa aaaa",
"dob":11-10-1988,
"workInformation":[
{
"address":"kolkata",
"workFor":"vvv Pvt Ltd",
"reference" : [
{
"refName" : "ttt",
"refId" : "12345"
},
{
"refName" : "sss",
"refId" : "23412"
}
]
},
{
"address":"bangalore",
"workFor":"www Pvt Ltd",
"reference" : [
{
"refName" : "rrr",
"refId" : "43434"
},
{
"refName" : "yyyy",
"refId" : "34213"
}
]
},
{
"address":"delhi",
"workFor":"sss Pvt Ltd",
"reference" : [
{
"refName" : "qqqq",
"refId" : "76767"
},
{
"refName" : "gggg",
"refId" : "65432"
}
]
}
]
}
I have tried insertFragment of DocumentPatchBuilder. 我尝试了DocumentPatchBuilder的insertFragment。 Using this i am able to update before/after of the json properties.
使用此,我能够在json属性之前/之后进行更新。 But i have to insert inside the property workInformation which is of array type.
但是我必须在数组类型的属性workInformation中插入。 Here is the insertFragment example which i tried -
这是我尝试过的insertFragment示例-
DocumentPatchBuilder pb = docMgr.newPatchBuilder();
pb.pathLanguage(DocumentPatchBuilder.PathLanguage.JSONPATH);
ObjectMapper mapper = new ObjectMapper();
pb.insertFragment("workInformation", Position.BEFORE,mapper.createObjectNode().put("hello", "hai"));
I wanted to insert data mentioned below inside the workInformation section using java api - 我想使用Java api将以下提到的数据插入到workInformation部分中-
{
"address":"Mumbai",
"workFor":"zzz Pvt Ltd"
}
Please let me know how to do it. 请让我知道该怎么做。
Thanks for Reading. 谢谢阅读。
To insert inside the workInformation array, you would need to specify the position as POSITION.LAST_CHILD to insert it as the last child of the child list of the context. 要插入workInformation数组内,您需要将位置指定为POSITION.LAST_CHILD才能将其插入到上下文的子级列表的最后一个子级中。 Also, you would need to specify "workInformation" as an array type in the first argument of patchBuilder.insertFragment - ["workInformation"].
另外,您需要在patchBuilder.insertFragment的第一个参数-[“ workInformation”]中将“ workInformation”指定为数组类型。 The code would be something like:
该代码将类似于:
DocumentPatchBuilder pb = docMgr.newPatchBuilder();
pb.pathLanguage(DocumentPatchBuilder.PathLanguage.JSONPATH);
ObjectMapper mapper = new ObjectMapper();
ObjectNode fragmentNode = mapper.createObjectNode();
fragmentNode = mapper.createObjectNode();
fragmentNode.put("address", "mumbai");
fragmentNode.put("workFor", "zzz Pvt Ltd");
String fragment = mapper.writeValueAsString(fragmentNode);
pb.insertFragment("$.[\"workInformation\"]", Position.LAST_CHILD, fragment);
This should do the trick and this would insert 这应该可以解决问题,并将其插入
{
"address":"Mumbai",
"workFor":"zzz Pvt Ltd"
}
at the end of "workInformation" property. 在“ workInformation”属性的末尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.