select a particular field from nested subdocuments Couchbase - couchbase

I have in a couchbase bucket documents having this structure:
"name": {
"grandfather": {
"parent1": {
"child1": [
{
.....
"uid": "value1",
},
{
"uid": "value2",
}
],
},
"parent2": {
"child2"
[
{
"uid": "value3",
}
],
}
}
}
I would need a query that returns
{
{
"uid": "value1",
},
{
"uid": "value2",
}
{
"uid": "value3",
}
}
.. intuitively something like:
select grandfather.*.*.uid from name;
but this one doesn't work. If someone can help, thank you

Across all the documents
WITH doc AS ( { "grandfather": { "parent1": { "child1": [ { "uid": "value1" }, { "uid": "value2" } ],
"child2": [{ "uid": "value3"}]
},
"parent2": { "child1": [ { "uid": "value4" }, { "uid": "value5" } ],
"child2": [{ "uid": "value6"}]
}
}
})
SELECT RAW c1
FROM doc AS a
UNNEST OBJECT_VALUES(a.grandfather) AS p
UNNEST OBJECT_VALUES(p) AS c
UNNEST c AS c1;
One entry per document
WITH doc AS ( { "grandfather": { "parent1": { "child1": [ { "uid": "value1" }, { "uid": "value2" } ],
"child2": [{ "uid": "value3"}]
},
"parent2": { "child1": [ { "uid": "value4" }, { "uid": "value5" } ],
"child2": [{ "uid": "value6"}]
}
}
})
SELECT ARRAY_FLATTEN(ARRAY (ARRAY cv FOR cn:cv IN pv END) FOR pn:pv IN a.grandfather END,3) AS names
FROM doc AS a;

Related

find and replace in json file using jq filter

I've below json file env.json and I want to search for "[\"res\",\"q3\"]" and replace it with a variable var1 value "[\"res\"]"
{
"idsb": "marqd",
"data": {
"name": "bcon-dv-alert"
},
"ingress": {
"args": {
"params": [
{
"name": "spt",
"value": "cld"
},
{
"name": "scv",
"value": "sdv"
},
{
"name": "scr",
"value": "ord"
}
{
"name": "srm",
"value": "[\"res\",\"q3\"]"
},
{
"name": "tgo",
"value": "pbc"
}
]
},
"wfr": {
"name": "t-r-e"
},
"snm": "as-r"
}
}
I tried the below way but it's not working
var1="[\"res\"]"
jq '.ingress.args.params[] | select(.name=="srm").value |= ["'${var1}'"]' env.json
where am making mistake? what's the right way to do it?
The final result will be
{
"idsb": "marqd",
"data": {
"name": "bcon-dv-alert"
},
"ingress": {
"args": {
"params": [
{
"name": "spt",
"value": "cld"
},
{
"name": "scv",
"value": "sdv"
},
{
"name": "scr",
"value": "ord"
}
{
"name": "srm",
"value": "[\"res\"]"
},
{
"name": "tgo",
"value": "pbc"
}
]
},
"wfr": {
"name": "t-r-e"
},
"snm": "as-r"
}
}
Since you want to update ingress, and not return only the result of the loops, use:
.ingress.args.params |= map(select(.name=="srm").value |= "new-value")
Try it online

Sort complex JSON object by specific property

How can I sort the given JSON object with property count. I want to sort the entire sub-object. The higher the count value should come on the top an so on.
{
"Resource": [
{
"details": [
{
"value": "3.70"
},
{
"value": "3.09"
}
],
"work": {
"count": 1
}
},
{
"details": [
{
"value": "4"
},
{
"value": "5"
}
],
"work": {
"count": 2
},
{
"details": [
{
"value": "5"
},
{
"value": "5"
}
],
"work": "null"
}
]
}
You can try this example to sort your data:
data = {
"data": {
"Resource": [
{
"details": [{"value": "3.70"}, {"value": "3.09"}],
"work": {"count": 1},
},
{"details": [{"value": "4"}, {"value": "5"}], "work": {"count": 2}},
]
}
}
# sort by 'work'/'count'
data["data"]["Resource"] = sorted(
data["data"]["Resource"], key=lambda r: r["work"]["count"]
)
# sort by 'details'/'value'
for r in data["data"]["Resource"]:
r["details"] = sorted(r["details"], key=lambda k: float(k["value"]))
# pretty print:
import json
print(json.dumps(data, indent=4))
Prints:
{
"data": {
"Resource": [
{
"details": [
{
"value": "3.09"
},
{
"value": "3.70"
}
],
"work": {
"count": 1
}
},
{
"details": [
{
"value": "4"
},
{
"value": "5"
}
],
"work": {
"count": 2
}
}
]
}
}

Is there any way to select all child array with just JsonPath

I have a json as below
{
"Shop": [
{
"id": "1",
"Items": [
{
"Item": "Item1"
}
]
},
{
"id": "2",
"Items": [
{
"Item": "Item2"
}
]
},
{
"id": "3",
"Items": [
{
"Item": "Item3"
}
]
}
]
}
I would like to select all Items with just JsonPath. I have tried as following combinations but I did not get any values
$.[Shop[0], Shop[1], Shop[2]].Items
$.[Shop[0].Items, Shop[1].Items, Shop[2].Items]
Thank you in advance
If I understand correctly you are looking for * wildcard to select all elements in an array:
$.Shop[*].Items
Gives me:
[ [
{
"Item": "Item1"
} ], [
{
"Item": "Item2"
} ], [
{
"Item": "Item3"
} ] ]

How to filter with console.log JSON?

I need to filter in the console.log the data that I get from the json that I show in the image. How could I do it?
Code
This is my .JSON
[
{
"category": "FORMULARIOS",
"items": [
{
"label": "Botones",
"url": "ui-botones"
},
{
"label": "Inputs",
"url": "ui-inputs"
}
]
},
{
"category": "CORREOS",
"items": [
{
"label": "Facturas",
"url": "ui-facturas"
},
{
"label": "Movimientos",
"url": "ui-movimientos"
}
]
},
{
"category": "ALERTAS",
"items": [
{
"label": "Toast",
"url": "ui-toast"
},
{
"label": "Toolips",
"url": "ui-toolips"
}
]
}
]
You can do like this:
var yourJSON ="......";
var newData = filterData('CORREOS');
function filterData(catalogyName) {
return yourJSON.filter(object => {
return object['category'] == catalogyName;
});
}
console.log(newData);

JSON query expression (JMESPath) to remove a key/value from a nested list of dictionaries

I have the below data structure in JSON, which is a dictionary where each element is a list of dictionaries.
{
"383e36d1-32e5-4705-8271-fa5e9e2ad538": [
{
"label": "blah",
"group_label": "group_a",
"id": "id_blah"
},
{
"label": "bloh",
"group_label": "group_b",
"id": "id_bloh"
}
],
"38b8293c-00c4-4bcf-91eb-440da656c653": [
{
"label": "bim",
"group_label": "group_c",
"id": "id_bim"
},
{
"label": "bam",
"group_label": "group_d",
"id": "id_bam"
},
...
}
and I need a JMESPath query expression to turn it into:
{
"383e36d1-32e5-4705-8271-fa5e9e2ad538": [
{
"label": "blah",
"group_label": "group_a",
},
{
"label": "bloh",
"group_label": "group_b",
}
],
"38b8293c-00c4-4bcf-91eb-440da656c653": [
{
"label": "bim",
"group_label": "group_c",
},
{
"label": "bam",
"group_label": "group_d",
},
...
}
Basically keeping the same structure but removing the id key from every entry
In Node.js, you could use the following code:
var json = {
"383e36d1-32e5-4705-8271-fa5e9e2ad538": [
{
"label": "blah",
"group_label": "group_a",
"id": "id_blah"
},
{
"label": "bloh",
"group_label": "group_b",
"id": "id_bloh"
}
],
"38b8293c-00c4-4bcf-91eb-440da656c653": [
{
"label": "bim",
"group_label": "group_c",
"id": "id_bim"
},
{
"label": "bam",
"group_label": "group_d",
"id": "id_bam"
}
]
};
for (var key in json) {
for (var index = 0; index < json[key].length; index++) {
delete json[key][index].id;
}
}
console.log(json);