How to extract JSON values that does not have attr

2019-09-28 06:29发布

问题:

{
 "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.