Marklogic - 使用 java api 在 json 的元素数组内更新
Marklogic - Update within an array of element of a json using java api
假设我们 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"
}
]
}
]
}
我尝试了 DocumentPatchBuilder 的 insertFragment。使用这个我可以更新 json 属性的 before/after。但是我必须在数组类型的 属性 workInformation 中插入。这是我试过的 insertFragment 示例 -
DocumentPatchBuilder pb = docMgr.newPatchBuilder();
pb.pathLanguage(DocumentPatchBuilder.PathLanguage.JSONPATH);
ObjectMapper mapper = new ObjectMapper();
pb.insertFragment("workInformation", Position.BEFORE,mapper.createObjectNode().put("hello", "hai"));
我想使用 java api -
在 workInformation 部分插入下面提到的数据
{
"address":"Mumbai",
"workFor":"zzz Pvt Ltd"
}
请告诉我怎么做。
感谢阅读。
要插入 workInformation 数组,您需要将位置指定为 POSITION.LAST_CHILD 以将其作为上下文子列表的最后一个子插入。此外,您需要在 patchBuilder.insertFragment - ["workInformation"] 的第一个参数中将 "workInformation" 指定为数组类型。代码类似于:
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);
这应该可以解决问题,这会插入
{
"address":"Mumbai",
"workFor":"zzz Pvt Ltd"
}
在 "workInformation" 的末尾 属性。
假设我们 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"
}
]
}
]
}
我尝试了 DocumentPatchBuilder 的 insertFragment。使用这个我可以更新 json 属性的 before/after。但是我必须在数组类型的 属性 workInformation 中插入。这是我试过的 insertFragment 示例 -
DocumentPatchBuilder pb = docMgr.newPatchBuilder();
pb.pathLanguage(DocumentPatchBuilder.PathLanguage.JSONPATH);
ObjectMapper mapper = new ObjectMapper();
pb.insertFragment("workInformation", Position.BEFORE,mapper.createObjectNode().put("hello", "hai"));
我想使用 java api -
在 workInformation 部分插入下面提到的数据 {
"address":"Mumbai",
"workFor":"zzz Pvt Ltd"
}
请告诉我怎么做。
感谢阅读。
要插入 workInformation 数组,您需要将位置指定为 POSITION.LAST_CHILD 以将其作为上下文子列表的最后一个子插入。此外,您需要在 patchBuilder.insertFragment - ["workInformation"] 的第一个参数中将 "workInformation" 指定为数组类型。代码类似于:
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);
这应该可以解决问题,这会插入
{
"address":"Mumbai",
"workFor":"zzz Pvt Ltd"
}
在 "workInformation" 的末尾 属性。