What's the correct way to select a value from an array of objects?
Would like to retrieve a distinct list of the comments.
[
"comment 1",
"comment 2",
"comment 3",
"comment 4"
]
Current N1QL query
SELECT links[*].comment FROM `my-db`
WHERE type = "links" AND ANY l IN links SATISFIES l.comment IS NOT MISSING END
Results
[
{
"comment": [
"comment 1"
]
},
{
"comment": [
"comment 1",
"comment 2"
]
},
{
"comment": [
"comment 3",
"comment 4"
]
}
]
Given a series of documents that contain a comment field:
{
"type": "links",
"links": [
{
"comment": "comment 1"
}
]
}
{
"type": "links",
"links": [
{
"comment": "comment 1"
},
{
"comment": "comment 2"
}
]
}
{
"type": "links",
"links": [
{
"comment": "comment 3"
},
{
"comment": "comment 4"
}
]
}
Use UNNEST
SELECT DISTINCT RAW l.comment
FROM `my-db` AS m
UNNEST m.links AS l
WHERE m.type = "links" AND l.comment IS NOT NULL;
OR
If data set is not too large.
SELECT RAW ARRAY_DISTINCT(ARRAY_FLATTEN(ARRAY_AGG(m.links[*].comment) ,2))
FROM `my-db` AS m
WHERE m.type = "links";
Related
I have an elastic search index
like
{
"title": "A",
"comments": [
{
"id": "1"
},
{
"id": "2"
}
]
},
{
"title": "B",
"comments": [
{
"id": "1"
},
{
"id": "3"
}
]
},
{
"title": "C",
"comments": [
{
"id": "7"
},
{
"id": "3"
}
]
}
I want to collapse is the group by the nested object. In the above JSON, I want to group it by Id.
So the output will be like
hits:[{
"title": "A",
"comments": [
{
"id": "1"
},
{
"id": "2"
}
]
},
inner_hits {[
{
"title": "A",
"comments": [
{
"id": "1"
},
{
"id": "2"
}
]
},
{
"title": "B",
"comments": [
{
"id": "1"
},
{
"id": "3"
}
]
}
]}
}]
Baiscally I need collapse bases on the nested object property.
Tried this
/_search?track_total_hits=true
{
"collapse": {
"field": "comments.id",
"inner_hits": {
"name": "id",
"size": 10
},
"max_concurrent_group_searches": 3
}
}
But its always returing first object only in the inner hits
Within the mapping of the object comments , you should remove the nested type.
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
I have a Json below which I am experiencing parse issues. I am not able to write sub array elements inside one array element. Need your help
{
"fulfillmentMessage": [{
"text": {
"text": [
"Element 1",
"Element 2",
"additionalCategory": [{
"link": "",
"followup_text": "Yes",
"title": "Yes"
},
{
"title": "No",
"followup_text": "No",
"link": ""
}
]
]
}
}]
}
For the above JSON, I am getting the below error
Error: Expecting Comma or ], not colon
You missed the curly braces before "additionalCategory".
Try this.
var json = {
"fulfillmentMessage": [{
"text": {
"text": [
"Element 1",
"Element 2",
{
"additionalCategory": [{
"link": "",
"followup_text": "Yes",
"title": "Yes"
},
{
"title": "No",
"followup_text": "No",
"link": ""
}
]
}
]
}
}]
};
console.log(json);
I am working with a JSON file which has contains lot of data that can be removed before sending to an API.
Found that JQ can be used to achieve this but not sure on how to map to get the desired results.
Input JSON
{
"name": "Sample name",
"id": "123",
"userStory": {
"id": "234",
"storyName": "Story Name",
"narrative": "Narrative",
"type": "feature"
},
"testSteps": [
{
"number": 1,
"description": "Step 1",
"level": 0,
"children": [
{
"number": 2,
"description": "Description",
"children": [
{
"number": 3,
"description": "Description"
}
]
},
{
"number": 4,
"anotherfield": "another field"
}
]
}
]
}
Desired Output
{
"name": "Sample name",
"userStory": {
"storyName": "Story Name"
},
"testSteps": [
{
"description": "Step 1",
"children": [
{
"description": "Description",
"children": [
{
"description": "Description"
}
]
},
{
"anotherfield": "anotherfield"
}
]
}
]
}
Tried to do it with the following jq command
map_values(..|{name, id, userStory})
but not sure how to filter only the userStory.storyName.
Thanks in advance.
Note: The actual JSON has different child elements that are repeated in some cases.
To delete .id from root object:
del(.id)
To leave only .storyName in .userStory:
.userStory |= {storyName}
To delete .number and .level from every object on any level in .testSteps:
.testSteps |= walk(if type == "object" then del(.number, .level) else . end)
Putting it all together:
del(.id) | (.userStory |= {storyName}) | (.testSteps |=
walk(if type == "object" then del(.number, .level) else . end))
Online demo
Given the following JSON,
I'm trying to filter out items in the cars array where the item's comments array is null:
.cars[] | select(.comments == null)
or the item's comments array exists and any of the comments objects don't contain the value "FooBar"
.cars[] | select( select(.comments != null) | (any(.comments[]; index("FooBar")) | not) )
while retaining the original structure.
With jq I can figure out how to create the critera I want to filter down to but what I can't wrap my head around is how to make that apply to the top level items.
JSON input:
{
"person": {
"first_name": "Bob",
"last_name": "Smith"
},
"addresses": [
{
"home": {
"line1": "123",
"line2": "A st."
}
},
{
"work": {
"line1": "456",
"line2": "B st."
}
}
],
"cars": [
{
"make": "Honda",
"model": "Civic"
},
{
"make": "Honda",
"model": "Accord"
},
{
"make": "Honda",
"model": "Pilot",
"comments": [
"Comment 1",
"Comment 2",
"FooBar"
]
},
{
"make": "Honda",
"model": "Passport",
"comments": [
"Comment 3",
"Comment 4"
]
}
]
}
JSON Outut, same as the input but the oject with the comment array containg the value "FooBar" is filtered out:
{
"person": {
"first_name": "Bob",
"last_name": "Smith"
},
"addresses": [
{
"home": {
"line1": "123",
"line2": "A st."
}
},
{
"work": {
"line1": "456",
"line2": "B st."
}
}
],
"cars": [
{
"make": "Honda",
"model": "Civic"
},
{
"make": "Honda",
"model": "Accord"
},
{
"make": "Honda",
"model": "Passport",
"comments": [
"Comment 3",
"Comment 4"
]
}
]
}
Just use the update-assignment operator (|=).
.cars |= map(select(.comments))
.cars |= map(select(.comments and any(.comments[]; index("FooBar")) | not))