1

您好,我有以下 json 字段(地址簿列表),我在其中尝试提取 addr1,但是我的代码结果为 NULL 有人可以帮忙吗?

询问-

select 
addressbooklist:addressbook.addressbookAddress.addr1 as test,
from customer;

JSON 字段

{
  "addressbook": [
    {
      "addressbookAddress": {
        "addr1": "701 N. Brand Boulevard",
        "addr2": null,
        "addr3": null,
        "addrPhone": null,
        "addrText": "Executive Software International<br>701 N. Brand Boulevard<br>Glendale CA 912031242<br>United States",
        "addressee": "Executive Software International",
        "attention": null,
        "city": "Glendale",
        "country": {
          "value": "_unitedStates"
        },
        "customFieldList": null,
        "internalId": null,
        "nullFieldList": null,
        "override": false,
        "state": "CA",
        "zip": "912031242"
      },
      "defaultBilling": false,
      "defaultShipping": true,
      "internalId": "1587207",
      "isResidential": false,
      "label": "701 N. Brand Boulevard"
    },
    {
      "addressbookAddress": {
        "addr1": "701 N. Brand Boulevard",
        "addr2": null,
        "addr3": null,
        "addrPhone": null,
        "addrText": "Executive Software International<br>701 N. Brand Boulevard<br>Glendale CA 912031242<br>United States",
        "addressee": "Executive Software International",
        "attention": null,
        "city": "Glendale",
        "country": {
          "value": "_unitedStates"
        },
        "customFieldList": null,
        "internalId": null,
        "nullFieldList": null,
        "override": false,
        "state": "CA",
        "zip": "912031242"
      },
      "defaultBilling": true,
      "defaultShipping": false,
      "internalId": "1587208",
      "isResidential": false,
      "label": "701 N. Brand Boulevard"
    }
  ],
  "replaceAll": false
}
4

3 回答 3

1

您的 json 文档addressbook元素是一个数组,而不是普通属性。[0]您可以使用符号(0=第一个元素,1=第二个,...)引用数组中的特定元素。您的查询将如下所示:

select addressbooklist:addressbook[0].addressbookAddress.addr1 as test from customer;

如果您想在结果中包含所有地址簿项目,您可以使用这种查询来展平 json 结构:

select ab.value:addressbookAddress.addr1 as test
from customer
, lateral flatten(input => addressbooklist:addressbook) ab;

结果:

TEST
"701 N. Brand Boulevard"
"701 N. Brand Boulevard"

可以在此处找到有关数组的雪花 json 语法的更多信息:

于 2021-08-31T18:33:35.563 回答
0

知道您想使用哪种语言会有所帮助。我只需要用 mySQL 来做这件事。这样的事情可能会有所帮助:

从 yourTable 中选择 yourField->>'$."addressbookAddress"[0].addr1' 作为地址,其中 1

于 2021-08-31T18:05:51.873 回答
0

您引用的JSON对象具有以下结构:

Array[Object1{key1: value, key2: value}, Object2{key1: value, key2: value}]

Object1是数组的第一项,索引为 0,Object2索引为 1,依此类推。

您可以访问数组中的一项,例如Array[0],您可以访问对象内部的值,例如Object1.key1. 在这个例子中,你会伸出手来key1点赞Array[0]Object1.key1

在您的情况下,您可以addr1像这样访问您的第一个对象:

addressbook[0].addressbookAddress.addr1
于 2021-09-03T08:36:57.380 回答