【问题标题】:Get values from JSON using JSON_VALUE in SQL Server在 SQL Server 中使用 JSON_VALUE 从 JSON 中获取值
【发布时间】:2021-10-14 23:18:44
【问题描述】:

我有以下 JSON(对不起,我不知道如何格式化它!),我正在努力理解如何在不同级别提取值。

到目前为止,我的代码是这样的,它从前几列中带回所需的数据..

  ,JSON_VALUE(jsonstring,'$[0].addPoint') Addpoint
  ,JSON_VALUE(jsonstring,'$[0].department') department
  ,JSON_VALUE(jsonstring,'$[0].subBuilding') subBuilding
  ,JSON_VALUE(jsonstring,'$[0].buildingNumber') Buildingnumber
  ,JSON_VALUE(jsonstring,'$[0].buildingGroup') buildingGroup

但是我不确定如何获得下面的列..

“mpan” “序列号”

有人可以告诉我我在这里缺少什么吗?我以前没有使用过 JSON 并且用谷歌搜索但找不到明确的解决方案

[
   {
      "addPoint":null,
      "department":null,
      "subBuilding":null,
      "buildingNumber":"1",
      "buildingName":null,
      "buildingGroup":null,
      "poBox":null,
      "subStreet":"The Arches",
      "subLocality":null,
      "stateRegion":"Lancashire",
      "subAdministrativeArea":null,
      "administrativeArea":null,
      "superAdministrativeArea":null,
      "countryCode":"GBR",
      "countryName":null,
      "dpsZipPlus":"1B5",
      "formattedAddress":"TEST,,MANC,Lancashire,66666",
      "welshSubStreet":null,
      "welshStreet":null,
      "welshSubLocality":null,
      "welshLocality":null,
      "welshTown":null,
      "geographicInformation":null,
      "additionalItems":{
         "item":[
            {
               "key":"DATASOURCE",
               "value":"tu_REGISTER"
            }
         ],
         "tmp":null
      },
      "groupedAdditionalItems":null,
      "persons":null,
      "uprn":null,
      "lpi":null,
      "blpu":null,
      "streetDescriptor":null,
      "streetInformation":null,
      "companyInformation":null,
      "dnbCompanyInformation":null,
      "onsPointerInformation":null,
      "classification":null,
      "osAl2Toid":null,
      "osItnToid":null,
      "osTopoToid":null,
      "voaCtRecord":null,
      "voaNdrRecord":null,
      "apOSAPR":null,
      "rmUDPRN":"2744498",
      "mrOccCountSpecified":false,
      "alias":null,
      "utilitiesInformation":{
         "fuelType":1,
         "fuelTypeSpecified":true,
         "gasInformation":null,
         "electricityInformation":{
            "meterPoint":[
               {
                  "mpan":"162558070",
                  "meter":[
                     {
                        "serialNumber":"D07W05001",
                        "type":"N"
                     }
                  ],
                  "profileType":"02",
                  "timeSwitchCode":"811",
                  "lineLossFactorId":"531",
                  "standardSettlementConfiguration":"0151",
                  "energisationStatus":"E",
                  "energisationEffectiveFromDate":{
                     "day":5,
                     "daySpecified":true,
                     "month":12,
                     "monthSpecified":true,
                     "year":2014,
                     "yearSpecified":true
                  },
                  "distributorId":"16",
                  "gspid":"_G"
               }
            ],
            "tmp":null
         },
         "tmp":null
      },
      "organisation":null,
      "street":"Clive Street",
      "town":"MANCH",
      "postCode":"r4d 1ES",
      "locality":null
   }]

【问题讨论】:

  • 如果您使用的是 SQL Server 2012,正如您所说,上述将出错;在 SQL Server 2017 中添加了JSON_VALUE
  • 对不起我的错误我现在已经编辑了这个
  • 那么如果不是 SQL Server,那么您使用的是什么 RDBMS?

标签: sql arrays json sql-server-2017


【解决方案1】:

您可以使用以下路径:

$[0].utilitiesInformation.electricityInformation.meterPoint[0].mpan
$[0].utilitiesInformation.electricityInformation.meterPoint[0].meter[0].serialNumber

【讨论】:

  • 完美..我现在完全理解了!很简单。谢谢你:)
猜你喜欢
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多