SELECT AS STRUCT 转为 JSON

huangapple go评论51阅读模式
英文:

SELECT AS STRUCT to json

问题

Is there a way to turn a STRUCT into JSON in the SELECT AS STRUCT construction in JSON? For example:

WITH tbl AS (
    SELECT "val.num" AS field_path, "INT64" AS data_type UNION ALL
    SELECT "val.str", "STRING"
) SELECT AS STRUCT field_path, data_type FROM tbl

How could I get this into the JSON value {"val.num": "INT64", "val.str": "STRING"}?

英文:

Is there a way to turn a STRUCT into json in the SELECT AS STRUCT construction in json? For example:

WITH tbl AS (
    SELECT "val.num" AS field_path, "INT64" AS data_type UNION ALL
    SELECT "val.str", "STRING"
) SELECT AS STRUCT field_path, data_type FROM tbl

How could I get this into the JSON value {"val.num": "INT64", "val.str": "STRING"} ?

答案1

得分: 0

Sure, here's the translated code portion:

> 如何将此内容转换为JSON值{"val.num": "INT64", "val.str": "STRING"}?

不确定是否有其他方法可以实现,但以下可能是一种可以用来生成您期望的输出的选项。

我已经在一个UDF中实现了它,该UDF将`键值对`作为参数,并返回`JSON`对象。

```sql
CREATE TEMP FUNCTION kv2json (pairs ARRAY<STRUCT<k STRING, v STRING>>) AS (( 
  SELECT PARSE_JSON(CONCAT('{"', STRING_AGG(k || '":"'|| v, '","'), '"}'))
    FROM UNNEST(pairs)
));

WITH tbl AS (
  SELECT "val.num" AS field_path, "INT64" AS data_type UNION ALL
  SELECT "val.str", "STRING"
)
SELECT kv2json(ARRAY_AGG(STRUCT(field_path, data_type)))
  FROM tbl;

SELECT AS STRUCT 转为 JSON


Note: This translation includes the code portion you provided, but without the code block formatting, which is not supported in this text-based format.

<details>
<summary>英文:</summary>

&gt; How could I get this into the JSON value {&quot;val.num&quot;: &quot;INT64&quot;, &quot;val.str&quot;: &quot;STRING&quot;} ?

Not sure there is other way to do it without string manipulation. Anyway below might be one option you can use to generate your expected output.

I&#39;ve implemented it in an UDF which takes `key-value pairs` as a parameter and returns `JSON` object.

```sql
CREATE TEMP FUNCTION kv2json (pairs ARRAY&lt;STRUCT&lt;k STRING, v STRING&gt;&gt;) AS ((
  SELECT PARSE_JSON(CONCAT(&#39;{&quot;&#39;, STRING_AGG(k || &#39;&quot;:&quot;&#39; || v, &#39;&quot;,&quot;&#39;), &#39;&quot;}&#39;))
    FROM UNNEST(pairs)
));

WITH tbl AS (
  SELECT &quot;val.num&quot; AS field_path, &quot;INT64&quot; AS data_type UNION ALL
  SELECT &quot;val.str&quot;, &quot;STRING&quot;
)
SELECT kv2json(ARRAY_AGG(STRUCT(field_path, data_type)))
  FROM tbl;

SELECT AS STRUCT 转为 JSON

答案2

得分: -1

以下是翻译好的部分:

也许最简单的方法是创建一个包含这些配对的数组。例如:

WITH tbl AS (
    SELECT "val.num" AS field_path, "INT64" AS data_type UNION ALL
    SELECT "val.str", "STRING"
)
SELECT ARRAY(SELECT AS STRUCT field_path, data_type FROM tbl)

如果您想将其转换为 json,可以使用 TO_JSON(ARRAY(...))

然而,这不是键/值形式,而是一组键-值对的数组。我不太确定如何将其转换为单个键/值对象。

英文:

Perhaps the simplest way would be to create an array of these pairs. For example:

WITH tbl AS (
    SELECT &quot;val.num&quot; AS field_path, &quot;INT64&quot; AS data_type UNION ALL
    SELECT &quot;val.str&quot;, &quot;STRING&quot;
)
SELECT ARRAY(SELECT AS STRUCT field_path, data_type FROM tbl)

And if you want to turn that into json, do it as TO_JSON(ARRAY(...)).

SELECT AS STRUCT 转为 JSON

However, this is not in k/v form but an array of key-values. I'm not quite sure how to get that into a single k/v object.

huangapple
  • 本文由 发表于 2023年5月22日 04:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定