[英]Java create a new Json reading from another Json using jsonpath
In java (any library), starting from a json like the following (with nested fields, arrays and embedded documents): 在java(任何库)中,从json开始,如下所示(带有嵌套字段,数组和嵌入文档):
{
"first" : "1",
"second" : {
"third" : "3",
"fourth" : "4"
},
"fifth" : [
{
"index" : 0,
"value": "something"
},
{
"index" : 1,
"value": "else"
}
]
}
and then apply following jsonpaths (as example, ideally any kind of jsonpath) 然后应用以下jsonpaths(例如,理想情况下任何类型的jsonpath)
second.fourth
fifth[1].[*]
create a json document like the following: 创建一个如下所示的json文档:
{
"second" : {
"fourth" : "4"
},
"fifth" : [
{
"index" : 1,
"value": "else"
}
]
}
So the question is: it's possible to use jsonpath not only to get data but also all nested fields and create a new json as subset of input? 所以问题是:可以使用jsonpath不仅可以获取数据,还可以使用所有嵌套字段并创建一个新的json作为输入的子集?
Any example is appreciated like always 任何例子都像往常一样被欣赏
Note : after thinking a lot about this, I've come to conclusion that what I need is an implementation of $project from Mongodb, but in Java and without a database. 注意 :经过深思熟虑后,我得出结论,我需要的是Mongodb的$ project实现,但是在Java中没有数据库。
If you can use lodash, there is set and get function for objects. 如果你可以使用lodash,那么对象就有set和get函数。
var source = {
"first" : "1",
"second" : {
"third" : "3",
"fourth" : "4"
},
"fifth" : [
{
"index" : 0,
"value": "something"
},
{
"index" : 1,
"value": "else"
}
]
};
var destination = {};
var path = "second.fourth";
_.set(destination, path, _.get(source, path));
path = "fifth[0]"; //Drawback -> if use fifth[1], then destination.fifth will have 0th element empty and 1st element filled.
_.set(destination, path, _.get(source, path));
Else you will need to write the javascript for these set and get operator. 否则,您需要为这些set和get运算符编写javascript。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.