I have a working query that flattens a nested JSON object into rows of data. What I would like to do, however, is preserve the original order of one array of objects which is nested several layers in.
I have tried to use ROW_NUMBER
with an ORDER BY NULL
and an ORDER BY (SELECT NULL)
and neither seem to preserve the order.
Any ideas on how to accomplish that? Examples below. I chose to mask the real data, but the important parts of the structure are the same. The data in JSON format comes through with no rank-identifying information, but I used numbers as examples here to show the strange results.
Original structure (masked):
{
"topNode: {
"childNode": {
"list": [
{
"title": "example title 1",
},
{
"title": "example title 2",
},
{
"title": "example title 3",
},
{
"title": "example title 4",
},
{
"title": "example title 5",
}
]
}
}
}
Example query (masked):
SELECT
A.VALUE:"title"::VARCHAR AS "TITLE",
ROW_NUMBER() OVER(ORDER BY NULL) AS RANK
FROM
DB.SCHEMA.TABLE as A,
lateral flatten(input=>A.JSON:topNode.childNode.list) "list_flatten"
Example output:
TITLE RANK
"example title 3" 1
"example title 5" 2
"example title 2" 3
"example title 1" 4
"example title 4" 5
It is possible with INDEX
, which returns index of element in array:
SELECT A.VALUE:"title"::VARCHAR AS "TITLE",
"list_flatten".index AS "RANK"
FROM DB.SCHEMA.TABLE as A,
lateral flatten(input=>A.JSON:topNode.childNode.list) "list_flatten"
I have a working query that flattens a nested JSON object into rows of data. What I would like to do, however, is preserve the original order of one array of objects which is nested several layers in.
I have tried to use ROW_NUMBER
with an ORDER BY NULL
and an ORDER BY (SELECT NULL)
and neither seem to preserve the order.
Any ideas on how to accomplish that? Examples below. I chose to mask the real data, but the important parts of the structure are the same. The data in JSON format comes through with no rank-identifying information, but I used numbers as examples here to show the strange results.
Original structure (masked):
{
"topNode: {
"childNode": {
"list": [
{
"title": "example title 1",
},
{
"title": "example title 2",
},
{
"title": "example title 3",
},
{
"title": "example title 4",
},
{
"title": "example title 5",
}
]
}
}
}
Example query (masked):
SELECT
A.VALUE:"title"::VARCHAR AS "TITLE",
ROW_NUMBER() OVER(ORDER BY NULL) AS RANK
FROM
DB.SCHEMA.TABLE as A,
lateral flatten(input=>A.JSON:topNode.childNode.list) "list_flatten"
Example output:
TITLE RANK
"example title 3" 1
"example title 5" 2
"example title 2" 3
"example title 1" 4
"example title 4" 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.