How to extract JSON values that does not have attribute names?

一世执手 提交于 2019-12-13 09:55:58

问题


{
 "A1":{
       "name":"Ad hoc",
       "projectId":0
      },
 "X2":{
       "name":"BBB",
       "projectId":101
        },
 "AB":{
       "name":"CCC",
       "projectId":102
        },
 "recordsCount":3
 }

For this JSON, how to extract values? i need output like,

 A1, Ad hoc
 X2, BBB
 AB, CCC

experts inputs appreciated.


回答1:


Analysis

XPath can't read unnamed attributes. It will always result in an exception. If you want to get the values, you need to use JsonPath.

Solution

Even then, it makes sense to add surrounding brackets, otherwise the first level will be consumed as well:

[
    {
 "A1":{
       "name":"Ad hoc",
       "projectId":0
      },
 "X2":{
       "name":"BBB",
       "projectId":101
        },
 "AB":{
       "name":"CCC",
       "projectId":102
        },
 "recordsCount":3
    }
]

You can try the correct query on jsonpath.com. For me (with the additional brackets) the path $.* worked.

To extract the values, you need to use a tExtractJSONFields component in Talend if using a file or REST request.

A valid JSON query could be easily [0] for the field with alphanumeric identifiers.



来源:https://stackoverflow.com/questions/38067057/how-to-extract-json-values-that-does-not-have-attribute-names

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