简体   繁体   English

如何从 JSON 元素中提取所有值?

[英]How do I extract all of the values from a JSON element?

How can I extract all of the values for the element account_code ?如何提取元素account_code的所有值? The below SELECT statement lets me extract any single value associated with index [x] but I want to extract all the values (each in it's own row) such that the output is:下面的SELECT语句让我提取与索引[x]关联的任何单个值,但我想提取所有值(每个值都在它自己的行中),这样 output 是:

account_codes
------------
1
2
3

SELECT
    JSON_EXTRACT_SCALAR(v, '$.accounting[0].account_code') AS account_codes
FROM (VALUES JSON '
    {"accounting":
        [
            {"account_code": "1", "account_name": "Travel"},
            {"account_code": "2", "account_name": "Salary"},
            {"account_code": "3", "account_name": "Equipment"},
        ]
    }'
) AS t(v)

The operator you need to use is unnest which will flatten the array and fetch you all the column values.您需要使用的运算符是 unnest ,它将展平数组并获取所有列值。 Below is the query and DDL in hive catalog I used to create table and fetch all account codes下面是 hive 目录中的查询和 DDL 我用来创建表并获取所有帐户代码

DDL: DDL:

CREATE EXTERNAL TABLE `sf_73515497`(
  `accounting` array<struct<account_code:string,account_name:string>> COMMENT 'from deserializer')
ROW FORMAT SERDE 
  'org.openx.data.jsonserde.JsonSerDe' 
WITH SERDEPROPERTIES ( 
  'paths'='accounting') 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  's3://path-to-json-prefix/'

SQL with unnest: SQL 与未嵌套:

WITH dataset AS (
  SELECT accounting from "sf_73515497" 
)
SELECT t.accounts.account_code FROM dataset
CROSS JOIN UNNEST(accounting) as t(accounts)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM