2

I'm trying to write a Couchbase query to get result from my JSON given below. I am using UNNEST operation to but somehow not getting expected result.

Can we use multiple UNNEST in one query? If so, then what is problem with my query?

Input JSON like below,

{
 "country": "India",
 "id": "1111",
 "value": {
 "isDefault": false,
 "schedules": {
 "default": {
 "startTime": null,
 "flightConfigs": {
 "1M": {
 "1": {
 "airlineid": "airline_5201",
 "destinationairport": "SFO",
 "name": "United Airlines",
 "sourceairport": "ABQ"
 },
 "2": {
 "airlineid": "airline_5202",
 "destinationairport": "SFO",
 "name": "United Airlines",
 "sourceairport": "ACV"
 }
 }
 },
 "2M": {
 "1": {
 "airlineid": "airline_5203",
 "destinationairport": "SFO",
 "name": "United Airlines",
 "sourceairport": "ABQ"
 },
 "2": {
 "airlineid": "airline_5204",
 "destinationairport": "SFO",
 "name": "United Airlines",
 "sourceairport": "ACV"
 }
 }
 }
 }
 }
}

I am expected output as below:

[
 {
 "id": "1111",
 "schedule": "default",
 "month": "1M" 
 },
 {
 "id": "1111",
 "schedule": "default",
 "month": "2M"
 }
]

This is the quest I'm trying to use, but I'm not getting th expected result:

select data.id as id, sched_n as schedule, month as month
from AIR_CONFIG.SCHEDULE.Flight_Schedule data
unnest OBJECT_NAMES(data.`value`.schedules) as sched_n
unnest OBJECT_INNER_VALUES(data.`value`.schedules) as sched
unnest OBJECT_NAMES(sched.flightConfigs) as month
where data.id like "1111"

Can someone please help on same.

Thanks

DarkBee
14.4k9 gold badges86 silver badges135 bronze badges
asked Jan 24, 2025 at 5:30
1
  • 1
    The JSON you've pasted isn't valid, so it's difficult to work out the query without guessing. Could you please update with a valid JSON value? Commented Jan 24, 2025 at 18:34

2 Answers 2

2

Instead of getting Names and Value in separate UNNSET we can read using UNNEST OBJECT_PAIRS

select data.id as id, sched_n.name as schedule, month.name as month from
AIR_CONFIG.SCHEDULE.Flight_Schedule data
UNNEST OBJECT_PAIRS(data.value.schedules) as sched_n
UNNEST OBJECT_PAIRS(sched.flightConfigs) as month 
where data.id like "1111"
DarkBee
14.4k9 gold badges86 silver badges135 bronze badges
answered Jan 27, 2025 at 5:44
Sign up to request clarification or add additional context in comments.

Comments

0
SELECT d.id AS id, u.*
FROM AIR_CONFIG.SCHEDULE.Flight_Schedule AS d
UNNEST ARRAY_FLATTENS((ARRAY (ARRAY {"schedule":sn, "month":fcn}
 FOR fcn:fcv IN sv.flightConfigs END)
 FOR sn:sv IN d.`value`.schedules END),1) AS u
WHERE d.id LIKE "1111";

OR

Different representation and more efficient

SELECT d.id AS id,
 ARRAY_FLATTENS((ARRAY (ARRAY {"schedule":sn, "month":fcn}
 FOR fcn:fcv IN sv.flightConfigs END)
 FOR sn:sv IN d.`value`.schedules END),1) AS schedules
FROM AIR_CONFIG.SCHEDULE.Flight_Schedule AS d
WHERE d.id LIKE "1111";
answered Feb 12, 2025 at 21:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.