de structure json nested object - json

I have the following json which are nested. I am looking for the better approach to read the data. Below is the sample json
[
{
"id": 1,
"name": "some text",
"selections": [
{
"id": 38383,
"items": [
{
"type": "view",
"children": [
{
"nodeId": "groups",
"children": [
{
"nodeId": "groups 1",
"children": [
{
"type": "group level item",
"name": "text1",
"leaf": true,
"info": [
"groups ",
"groups 1",
"groups 2"
]
},
{
"type": "group level item",
"name": "text2",
"leaf": true,
"info": [
"groups ",
"groups 1",
"groups 2"
]
}
]
}
]
}
]
}
]
}
]
},
{
"id": 2,
"name": "some text",
"selections": [
{
"id": 23232,
"items": [
{
"type": "view",
"children": [
{
"nodeId": "Hirearchy",
"children": [
{
"nodeId": "Hirearchy level 1",
"children": [
{
"nodeId": "Hirearchy level 2",
"children": [
{
"type": "charactreistic value",
"name": "some text 1",
"leaf": true,
"info": [
"Hirearchy",
"Hirearchy level 1",
"Hirearchy level 2",
"Hirearchy level 3"
]
},
{
"type": "characterisitic value",
"name": "some text 2",
"leaf": true,
"info": [
"Hirearchy",
"Hirearchy level 1",
"Hirearchy level 2",
"Hirearchy level 3"
]
}
]
}
]
}
]
}
]
}
]
}
]
},
{
"id": 3,
"name": "some text",
"selections": [
{
"id": 2444,
"items": [
{
"type": "view",
"children": [
{
"nodeId": "category",
"children": [
{
"type": "some value",
"name": "some text 1",
"leaf": true,
"info": [
"category",
"category level 1"
]
},
{
"type": "some value",
"name": "some text 2",
"leaf": true,
"info": [
"category",
"category level 1"
]
}
]
}
]
}
]
}
]
},
{
"id": 4,
"name": "some text",
"selections": [
{
"id": 2444,
"items": [
{
"type": "view",
"children": []
}
]
}
]
},
{
"id": 5,
"name": "some text",
"selections": [
{
"id": 2444,
"items": []
}
]
},
{
"id": 6,
"name": "some text",
"selections": []
}
]
for id 1 - there is 3 levels of nesting and the last one has a property which says leaf = true. so in this i need to de structure the object to get the desire out put like
heading - group 2 (from info arrar)
name - ['text1', 'text2'] from the last leaf
for id 2 - there is 4 levels of nesting and the last one has a property which says leaf = true. so in this i need to de structure the object to get the desire out put like
heading - Hierarchy level 3
name - ['some text1', 'some text2']
for id 3 there is 2 levels of nesting and the last one has a property which says leaf = true. so i this
i need to se structure the object to get the desire output like this
heading - Category 1
name - ['some text 3', 'some text4']
for id 4 - there is 1 level of nesting but the array is blank
for id 5 - there is no nesting and the items array is blank
for id 6 - selections array is blank
Question 1. I need to write a generic method to loop thru the json object and find the node where the property 'leaf' is true.
Question 2. This generic method to work even if the nesting levels are 2 or 3 or 4 or it could be more also
Question 3. This generic method should not break the code when the array is empty also.
Requirement. this is required in angular 10 application where i need to destructure the object
Thanks in advance

Related

JQ - unique count of each value in an array

I'm needing to solve this with JQ. I have a large lists of arrays in my json file and am needing to do some sort | uniq -c types of stuff on them. Specifically I have a relatively nasty looking fruit array that needs to break down what is inside. I'm aware of unique and things like that, and imagine there is likely a simple way to do this, but I've been trying run down assigning things as variables and appending and whatnot, but I can't get the most basic part of counting the unique values per that fruit array, and especially not without breaking the rest of the content (hence the variable ideas). Please tell me I'm overthinking this.
I'd like to turn this;
[
{
"uid": "123abc",
"tID": [
"T19"
],
"fruit": [
"Kiwi",
"Apple",
"",
"",
"",
"Kiwi",
"",
"Kiwi",
"",
"",
"Mango",
"Kiwi"
]
},
{
"uid": "456xyz",
"tID": [
"T15"
],
"fruit": [
"",
"Orange"
]
}
]
Into this;
[
{
"uid": "123abc",
"tID": [
"T19"
],
"metadata": [
{
"name": "fruit",
"value": "Kiwi - 3"
},
{
"name": "fruit",
"value": "Mango - 1"
},
{
"name": "fruit",
"value": "Apple - 1"
}
]
},
{
"uid": "456xyz",
"tID": [
"T15"
],
"metadata": [
{
"name": "fruit",
"value": "Orange - 1"
}
]
}
]
Using group_by and length would be one way:
jq '
map(with_entries(select(.key == "fruit") |= (
.value |= (group_by(.) | map(
{name: "fruit", value: "\(.[0] | select(. != "")) - \(length)"}
))
| .key = "metadata"
)))
'
[
{
"uid": "123abc",
"tID": [
"T19"
],
"metadata": [
{
"name": "fruit",
"value": "Apple - 1"
},
{
"name": "fruit",
"value": "Kiwi - 4"
},
{
"name": "fruit",
"value": "Mango - 1"
}
]
},
{
"uid": "456xyz",
"tID": [
"T15"
],
"metadata": [
{
"name": "fruit",
"value": "Orange - 1"
}
]
}
]
Demo

Search object a element and return the element and parents

I want to make a search from object JSON but when a I found the element I want to be returned with his previous parents.
[
{
"name": "level1",
"children": [
{
"name": "level2",
"children": [
{
"name": "level3a",
"children": [
{
"name": "text",
"more": "info"
},
{
"name": "text Abc",
"more": "info"
}
]
},
{
"name": "level3b",
"children": [
{
"name": "text-C",
"more": "info"
},
{
"name": "search",
"more": "info"
}
]
},
{
"name": "level3c",
"children": [
{
"name": "info-C",
"more": "info"
},
{
"name": "search",
"more": "info"
}
]
}
]
}
]
},
{
"name": "level1",
"children": [
{
"name": "level2a",
"children": [
{
"name": "level3",
"children": [
{
"name": "text A",
"more": "info"
}
]
},
]
},
{
"name": "level2b",
"children": [
{
"name": "level3",
"children": [
{
"name": "text X",
"more": "info"
}
]
},
]
}
]
}
]
For example: if want to search "text" I want result like this
[
{
"name": "level1",
"children": [
{
"name": "level2",
"children": [
{
"name": "level3a",
"children": [
{
"name": "text",
"more": "info"
},
{
"name": "text Abc",
"more": "info"
}
]
},
{
"name": "level3b",
"children": [
{
"name": "text-C",
"more": "info"
}
]
}
]
}
]
},
{
"name": "level1",
"children": [
{
"name": "level2a",
"children": [
{
"name": "level3",
"children": [
{
"name": "text A",
"more": "info"
}
]
},
]
},
{
"name": "level2b",
"children": [
{
"name": "level3",
"children": [
{
"name": "text X",
"more": "info"
}
]
},
]
}
]
}
]
Things to considerer
only "search" in level 3 children by name property
if one o more childrens matches the search, return all of them
Basically I need to filter by the last level of object with elements matches with the search and return them with them parents.

POWERSHELL - How to access multilevel child elements in JSON file with condtion

can someone please send me solution or link for PowerShell 5 and 7 how can I access child elements if specific condition is fulfilled for JSON file which I have as output.json. I haven't find it on the net.
I want to retrieve value of the "children" elements if type element has value FILE and to put that into some list. So final result should be [test1.txt,test2.txt]
Thank you!!!
{
"path": {
"components": [
"Packages"
],
"parent": "",
"name": "Packages",
},
"children": {
"values": [
{
"path": {
"components": [
"test1.txt"
],
"parent": "",
"name": "test1.txt",
},
"type": "FILE",
"size": 405
},
{
"path": {
"components": [
"test2.txt"
],
"parent": "",
"name": "test2.txt",
},
"type": "FILE",
"size": 409
},
{
"path": {
"components": [
"FOLDER"
],
"parent": "",
"name": "FOLDER",
},
"type": "DIRECTORY",
"size": 1625
}
]
"start": 0
}
}
1.) The json is incorrect, I assumt that this one is the correct one:
{
"path": {
"components": [
"Packages"
],
"parent": "",
"name": "Packages"
},
"children": {
"values": [
{
"path": {
"components": [
"test1.txt"
],
"parent": "",
"name": "test1.txt"
},
"type": "FILE",
"size": 405
},
{
"path": {
"components": [
"test2.txt"
],
"parent": "",
"name": "test2.txt"
},
"type": "FILE",
"size": 409
},
{
"path": {
"components": [
"FOLDER"
],
"parent": "",
"name": "FOLDER"
},
"type": "DIRECTORY",
"size": 1625
}
],
"start": 0
}
}
2.) The structure is not absolute clear, but for your example this seems to me to be the correct solution:
$element = $json | ConvertFrom-Json
$result = #()
$element.children.values | foreach {
if ($_.type -eq 'FILE') { $result += $_.path.name }
}
$result | ConvertTo-Json
Be aware, that the used construct $result += $_.path.name is fine if you have up to ~10k items, but for very large items its getting very slow and you need to use an arraylist. https://adamtheautomator.com/powershell-arraylist/

How to loop through a JSON object with typescript

I use angular 7.
my query return a json data which has this format :
[
{
"text": "test 1",
"value": "1",
"nbr": "1",
"children": [
{
"text": "test 1_1",
"value": "1_1",
"nbr": "2",
"children": [
{
"text": "test 1_1_1",
"value": "1_1_1",
"nbr": "1",
"children": []
},
{
"text": "test 1_1_2"",
"value": "1_1_2",
"nbr": "0",
"children": []
},
{
"text": "test 1_1_3"",
"value": "1_1_3",
"nbr": "0",
"children": []
}
]
},
{
"text": "test 1_2",
"value": "1_2",
"nbr": "0",
"children": []
}
]
},
{
"text": "test 2",
"value": "2",
"nbr": "0",
"children": []
}
]
I want to loop this data and essentially loop the children data.
and I want to make some test.
I try with this code which has problem to loop children data.
this.httpservice.query({
}).subscribe((res: HttpResponse<TestEntity[]>) => {
this.temp= res.body;
this.temp.forEach((x) => {
x["children"].forEach(x => {
if(x.nbr=='0')
{
// test code
}
x["children"].forEach(x => {
if(x.nbr=='0')
{
// test code
}
})
})
});
});
I didn't find the way to loop the children data .
Any help would be greatly appreciated
Should be something like:
const objects = Object.keys(data).map(key => data[key]);

How to convert flat array of objects with "categories" and "subcategories" to tree with JQ?

I have a JSON array of objects with groups and subgroups that looks like this:
[
{
"Group name": "Elevation",
"Subgroup": "Contours",
"name": "Contours - Labels"
},
{
"Group name": "Elevation",
"Subgroup": "Contours",
"name": "Contours"
},
{
"Group name": "Elevation",
"Subgroup": "Cuttings",
"name": "Cuttings"
},
{
"Group name": "Framework",
"Subgroup": "Reserves",
"name": "Reserves"
},
{
"Group name": "Framework",
"Subgroup": "Indigenous Reserves",
"name": "Reserves"
},
{
"Group name": "Framework",
"Subgroup": "Land Borders",
"name": "Mainland"
}
]
I'd like to convert it to a nested structure:
[ { type: group, name: Elevation, Items: [ ... ] } ]
How do you do this in JQ?
I'm not sure what your expected output is exactly, but this could be done more efficiently. By parameterizing your grouping function, you could build up your trees in a reusable fashion.
def regroup(keyfilter; itemfilter):
group_by(keyfilter) | map({
type: "group",
name: (.[0] | keyfilter),
items: itemfilter
})
;
regroup(."Group name";
regroup(.Subgroup;
map({ name })
)
)
This yields the following results:
[
{
"type": "group",
"name": "Elevation",
"items": [
{
"type": "group",
"name": "Contours",
"items": [
{ "name": "Contours - Labels" },
{ "name": "Contours" }
]
},
{
"type": "group",
"name": "Cuttings",
"items": [
{ "name": "Cuttings" }
]
}
]
},
{
"type": "group",
"name": "Framework",
"items": [
{
"type": "group",
"name": "Indigenous Reserves",
"items": [
{ "name": "Reserves" }
]
},
{
"type": "group",
"name": "Land Borders",
"items": [
{ "name": "Mainland" }
]
},
{
"type": "group",
"name": "Reserves",
"items": [
{ "name": "Reserves" }
]
}
]
}
]
Functions are very powerful, you should try to utilize it more. Hopefully this shows just how powerful it can be.
This did the job:
def subgroups:
group_by(."Subgroup")|
map({
"type": "group",
"name": .[0]."Subgroup",
"items": (map(del(.Subgroup)))
});
group_by(."Group name")|
map({
"type": "group",
"name": .[0]."Group name",
"items": (subgroups2|map(del(."Group name")))
})
It's just the same process twice:
group_by to sort the outer items on "group name"
For each of those groups, construct an object whose name is taken from the first item, and whose items are...
Passed through to a function that repeats 1 and 2, and removes the grouping field.