首页 > 解决方案 > 在 SQL Server 中使用 JSON_VALUE 从 JSON 中获取值

问题描述

我有下面的 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
   }]

标签: sqlarraysjsonsql-server-2017

解决方案


您可以使用以下路径:

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

推荐阅读