Extract and Crete new JSON string

可紊 提交于 2020-02-25 05:20:30

问题


I have some spring MVC based APIs which produces JSON/ XML which represents API output.

{
    "data" : 
    {
  "users": [
            {
               "id": "001",
                "name: "abc1",
                "type": {
                      "id": "P",
                       "name": "Permanent"
                 }
            },
           {
                "id": "002",
                "name: "xyz",
                "type": {
                       "id": "C",
                       "name": "Contractor"
                 }
           }
     ]
    }
}

I'm passing a parameter with request as url?fields=users.id, users.type.id

users.type.id is a sub-node in users node. users node is an array.

Now, what I want to do is to filter those only properties and create the response based upon fields passed in the request.

So the response to above filter condition should be same structure and will only contain wanted fields with values. I'm trying to build a flat map with keys with a dot notation so I won't lose the track to filter, then I'll rebuild the JSON again. I feel this approach is just unreasonable because Jackson has the .path and .with API's to check existing path. But the real challenge is to extract and create a new JSON which matches the response JSON.

I'm looking for some ideas to achieve this. I don't want to try any third party libs btw. I know some libs are there. I want to prefer Jackson way to do this.

Feel free to add or comment if you have a further ideas.

来源:https://stackoverflow.com/questions/49361464/extract-and-crete-new-json-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!