在bigQuery中使用sql选择json键的值

ndh0cuux  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(133)

我运行一个简单的查询,返回如下输出:

[{
  "polarity": "0.0",
  "magnitude": "2.0",
  "score": "0.5",
  "entities": [{
    "name": "Taubenkot",
    "type": "OTHER",
    "mid": "",
    "wikipediaUrl": "",
    "numMentions": "1",
    "avgSalience": "0.150263"
  }, {
    "name": "Lösung",
    "type": "OTHER",
    "mid": "",
    "wikipediaUrl": "",
    "numMentions": "1",
    "avgSalience": "0.145794"
  }, {
    "name": "Busbahnhof",
    "type": "LOCATION",
    "mid": "",
    "wikipediaUrl": "",
    "numMentions": "1",
    "avgSalience": "0.108006"
  }, {
    "name": "Stadt",
    "type": "LOCATION",
    "mid": "",
    "wikipediaUrl": "",
    "numMentions": "3",
    "avgSalience": "0.079928"
  }, {
    "name": "Taubenplage",
    "type": "OTHER",
    "mid": "",
    "wikipediaUrl": "",
    "numMentions": "2",
    "avgSalience": "0.072326"
  }, {
    "name": "Rutschgefahr",
    "type": "OTHER",
    "mid": "",
    "wikipediaUrl": "",
    "numMentions": "1",
    "avgSalience": "0.070839"
  },
{
    "name": "Meike Böschemeyer",
    "type": "PERSON",
    "mid": "/g/11btlnnjgf",
    "wikipediaUrl": "",
    "numMentions": "1",
    "avgSalience": "0.00451"
  }]
}]

BigQuery自动在单独的列中显示不同的实体键值。如何查询特定键值?
我在试这个:

select JSON_VALUE(entities, '$.type') AS type from gcnlapi limit 1

但它给了我一个错误

No matching signature for function JSON_VALUE for argument types: ARRAY<STRUCT<name STRING, type STRING, mid STRING, ...>>, STRING. Supported signatures: JSON_VALUE(STRING, [STRING]); JSON_VALUE(JSON, [STRING]) at [3:8]

还尝试了以下操作:

select entities.type AS type from gcnlapi limit 1

但那会给予我

Cannot access field type on a value with type ARRAY<STRUCT<name STRING, type STRING, mid STRING, ...>> at [5:17]
lb3vh1jj

lb3vh1jj1#

我希望这个例子能帮助你:

SELECT
    polarity,
    magnitude,
    score,

    name,
    type,
    mid,
    wikipediaUrl,
    numMentions,
    avgSalience
FROM
    `your_project.your_dataset.your_table`,
    UNNEST(entities)

使用UNNEST,您可以flatten您的数组,并获得表的root级别的字段以及数组的字段(扁平化)。
在本查询中,根级字段为polaritymagnitudescore,其余对应数组字段。

相关问题