SQL: JSON 从嵌套对象中提取
SQL: JSON Extract from nested object
我有一个table这样的
id
对象
1
{"is_from_shopping_bag":true,"products":[{"price":{"amount":"18.00","currency":"USD","offset":100," amount_with_offset":"1800"},"product_id":"1234","数量":1}],"来源":"购物车"}
2
{"is_from_shopping_bag":false,"products":[{"price":{"amount":"80.00","currency":"USD","offset":100," amount_with_offset":"8000"},"product_id":"2345","数量":1}],"来源":"pdp"}
我正在 Hive 中执行 sql 查询以获取 'currency' 字段。
目前我可以运行
SELECT
JSON_EXTRACT( obj, '$.products')
FROM my_table
哪个returns
对象
[{"price":{"amount":"18.00","currency":"USD","offset":100,"amount_with_offset":"1800"},"product_id":"1234","数量":1}]
[{"price":{"amount":"80.00","currency":"USD","offset":100,"amount_with_offset":"8000"},"product_id":"2345","数量":1}]
如何更深入地获取货币?
要获取第一个产品使用的货币:
SELECT
id,JSON_EXTRACT( obj, '$.products[0].price.currency') first_product_currency
FROM my_table;
id
first_product_currency
1
"USD"
2
"USD"
要获取所有产品的货币,请使用:
SELECT
id,JSON_EXTRACT( obj, '$.products[*].price.currency') multiple_currencies
FROM my_table;
id
multiple_currencies
1
["USD"]
2
["USD"]
我有一个table这样的
id | 对象 | |||
---|---|---|---|---|
1 | {"is_from_shopping_bag":true,"products":[{"price":{"amount":"18.00","currency":"USD","offset":100," amount_with_offset":"1800"},"product_id":"1234","数量":1}],"来源":"购物车"} | |||
2 | {"is_from_shopping_bag":false,"products":[{"price":{"amount":"80.00","currency":"USD","offset":100," amount_with_offset":"8000"},"product_id":"2345","数量":1}],"来源":"pdp"} | |||
我正在 Hive 中执行 sql 查询以获取 'currency' 字段。
目前我可以运行
SELECT
JSON_EXTRACT( obj, '$.products')
FROM my_table
哪个returns
对象 | |||
---|---|---|---|
[{"price":{"amount":"18.00","currency":"USD","offset":100,"amount_with_offset":"1800"},"product_id":"1234","数量":1}] | |||
[{"price":{"amount":"80.00","currency":"USD","offset":100,"amount_with_offset":"8000"},"product_id":"2345","数量":1}] | |||
如何更深入地获取货币?
要获取第一个产品使用的货币:
SELECT
id,JSON_EXTRACT( obj, '$.products[0].price.currency') first_product_currency
FROM my_table;
id | first_product_currency |
---|---|
1 | "USD" |
2 | "USD" |
要获取所有产品的货币,请使用:
SELECT
id,JSON_EXTRACT( obj, '$.products[*].price.currency') multiple_currencies
FROM my_table;
id | multiple_currencies |
---|---|
1 | ["USD"] |
2 | ["USD"] |