How to Get the Parent Element Name of a Child Element in Json

How to get the parent element name of a child element in JSON?

There is an option with the JsonPath library that returns the entire matched paths instead of the values. Thus you can do something like:

Configuration conf = Configuration.builder().options(Option.AS_PATH_LIST).build();
List<String> pathList = JsonPath.using(conf).parse(payload).read("$..ppsNo");

/* Returns :
* [
* "$['contractor']['ppsNo']",
* "$['fullTimeStaff']['ppsNo']"
* ]
*/

You just have to parse the result to the correct type, and remove the last element to get the direct parent.

Pattern pattern = Pattern.compile("(?<!\\$\\[)(\\w+)(?!\\])");
pathList = pathList.stream().map(path -> {
Matcher m = pattern.matcher(path.toString());
return m.find() ? m.group(0) : null;
}).collect(Collectors.toList());
System.out.println(pathList); // [contractor, fullTimeStaff]

Here is the link to the official Jayway JsonPath Maven repository.

How to get list of parents id in json object

You need to do recursive search

 const persons = [
{
id: 1,
name: "Mike",
children: [
{ id: 2, name: "MikeC1" },
{ id: 3, name: "MikeC2" },
{
id: 4, name: "MikeC3",
children: [{ id: 5, name: "MikeCC1" }]
},
]
},
{
id: 6,
name: "Json",
children: [
{ id: 7, name: "JsonC1" },
{ id: 8, name: "JsonC2" },
{
id: 9, name: "JsonC3",
children: [{ id: 10, name: "JsonCC1" },{ id: 11, name: "JsonCC2" }]
},
]
}
];

function searchRecursive(items, id) {
const allIds = [];

items.forEach(item => {
if(item.id === id) {
allIds.push(item.id);
}
else if(item.children) {
const ids = searchRecursive(item.children, id);

if(ids.length) allIds.push(item.id);

ids.forEach(id => allIds.push(id));
}
});

return allIds;
}

console.log(searchRecursive(persons, 11));

Get value of parent element by child jq

Assuming your input JSON is

{
"endpointAgents": [
{
"agentId": "MyId",
"agentName": "MYNAME",
"location": {
"locationName": "location"
},
"clients": [
{
"userProfile": {
"userName": "Name"
},
"browserExtensions": [
{
"active": false
}
]
}
],
"totalMemory": "16222 MB",
"agentType": "enterprise"
}
]
}

To get the agentId values from all items of the endpointAgents array where in the same object at least one object in the clients array has a userProfile.userName string value that contains a given substring, I'd go with

jq -r '
.endpointAgents[]
| select(.clients | map(.userProfile.userName | contains("a")) | any)
| .agentId
'
MyId

Demo

In order import the query string from outside jq, use the --arg parameter

jq -r --arg query "a" ' … contains($query) … '

Get the id of a parent element by a sub-child value of Json response using groovy

You can use:

json.items.find({ it.name == "LAI-00152581" })?.id

?. is for safety when there is no items meeting the criteria. In which case the result will be null

Since Groovy 2.5.0 there is one more way of doing this, which is semantically equivalent:

json.items.findResult { if (it.name == "LAI-00152581") return it.id }

JSONPath get the id of a parent element by a sub-child value

I could only solve the first part of my question by using:

$['datapoints'][*][?(@['default.name']=='Delta_K')]

During my research I found that jsonpath does not support to get the parent of a filtered node. In Chapter 7 "Conclusion" of http://www.baeldung.com/guide-to-jayway-jsonpath it's written:

Although JsonPath has some drawbacks, such as a lack of operators for reaching parent or sibling nodes, it can be highly useful in a lot of scenarios.

Also further SO posts couldn't help me.

  • Getting parent of matched element with jsonpath
  • Using jsonpath to get parent node

How to get immediate parent Id of the child id in array of nested json Object?

Replace this:

t.push(arr[i].id);

with:

if (t.length == 0) t.push(arr[i].id);


Related Topics



Leave a reply



Submit