SQL Server 将 JSON 对象解析为行

如何解决SQL Server 将 JSON 对象解析为行

在此先感谢您的帮助。这要死我了! :-)

我有一个 JSON 字符串,其中嵌套了一个对象集合,我需要从中获取标准 SQL 结果集。这是我正在使用的 JSON 的示例字符串:

    {
        "ST": {
            "TransactionSetIdentifierCode_01": "835","TransactionSetControlNumber_02": "1740","ImplementationConventionPreference_03": null
        },"BPR_FinancialInformation": {
            "TransactionHandlingCode_01": "I","TotalPremiumPaymentAmount_02": "45.75","CreditorDebitFlagCode_03": "C","PaymentMethodCode_04": "ACH","PaymentFormatCode_05": "CCP","DepositoryFinancialInstitutionDFIIdentificationNumberQualifier_06": "01","OriginatingDepositoryFinancialInstitutionDFIIdentifier_07": "111","AccountNumberQualifier_08": "DA","SenderBankAccountNumber_09": "33","PayerIdentifier_10": "1234567890","OriginatingCompanySupplementalCode_11": null,"DepositoryFinancialInstitutionDFIIdentificationNumberQualifier_12": "01","ReceivingDepositoryFinancialInstitutionDFIIdentifier_13": "111","AccountNumberQualifier_14": "DA","ReceiverBankAccountNumber_15": "22","CheckIssueorEFTEffectiveDate_16": "20100101","BusinessFunctionCode_17": null,"DFIIDNumberQualifier_18": null,"DFIIdentificationNumber_19": null,"AccountNumberQualifier_20": null,"AccountNumber_21": null
        },"TRN_ReassociationTraceNumber": {
            "TraceTypeCode_01": "1","CurrentTransactionTraceNumber_02": "10100000000","OriginatingCompanyIdentifier_03": "1000000000","ReferenceIdentification_04": null
        },"CUR_ForeignCurrencyInformation": null,"AllREF": {
            "REF_ReceiverIdentification": {
                "ReferenceIdentificationQualifier_01": "EV","MemberGrouporPolicyNumber_02": "ETIN","Description_03": null,"ReferenceIdentifier_04": null
            },"REF_VersionIdentification": null
        },"DTM_ProductionDate": {
            "DateTimeQualifier_01": "405","Date_02": "20100101","Time_03": null,"TimeCode_04": null,"DateTimePeriodFormatQualifier_05": null,"DateTimePeriod_06": null
        },"AllN1": {
            "Loop1000A": {
                "N1_PayerIdentification": {
                    "EntityIdentifierCode_01": "PR","PremiumPayerName_02": "NYSDOH","IdentificationCodeQualifier_03": null,"IntermediaryBankIdentifier_04": null,"EntityRelationshipCode_05": null,"EntityIdentifierCode_06": null
                },"N3_PayerAddress": {
                    "ResponseContactAddressLine_01": "OFFICE OF HEALTH INSURANCE PROGRAMS","ResponseContactAddressLine_02": "CORNING TOWER,EMPIRE STATE PLAZA"
                },"N4_PayerCity_State_ZIPCode": {
                    "AdditionalPatientInformationContactCityName_01": "ALBANY","AdditionalPatientInformationContactStateCode_02": "NY","AdditionalPatientInformationContactPostalZoneorZIPCode_03": "122370080","CountryCode_04": null,"LocationQualifier_05": null,"LocationIdentifier_06": null,"CountrySubdivisionCode_07": null
                },"REF_AdditionalPayerIdentification": null,"AllPER": {
                    "PER_PayerBusinessContactInformation": null,"PER_PayerTechnicalContactInformation": [
                        {
                            "ContactFunctionCode_01": "BL","ResponseContactName_02": "PROVIDER SERVICES","CommunicationNumberQualifier_03": "TE","ResponseContactCommunicationNumber_04": "8003439000","CommunicationNumberQualifier_05": "UR","ResponseContactCommunicationNumber_06": "www.emedny.org","CommunicationNumberQualifier_07": null,"ResponseContactCommunicationNumber_08": null,"ContactInquiryReference_09": null
                        }
                    ],"PER_PayerWEBSite": null
                }
            },"Loop1000B": {
                "N1_PayeeIdentification": {
                    "EntityIdentifierCode_01": "PE","PremiumPayerName_02": "MAJOR MEDICAL PROVIDER","IdentificationCodeQualifier_03": "XX","IntermediaryBankIdentifier_04": "9999999995","N3_PayeeAddress": null,"N4_PayeeCity_State_ZIPCode": null,"REF_PayeeAdditionalIdentification": [
                    {
                        "ReferenceIdentificationQualifier_01": "TJ","MemberGrouporPolicyNumber_02": "000000000","ReferenceIdentifier_04": null
                    }
                ],"RDM_RemittanceDeliveryMethod": null
            }
        },"Loop2000": [
            {
                "LX_HeaderNumber": {
                    "AssignedNumber_01": "1"
                },"TS3_ProviderSummaryInformation": null,"TS2_ProviderSupplementalSummaryInformation": null,"Loop2100": [
                    {
                        "CLP_ClaimPaymentInformation": {
                            "PatientControlNumber_01": "PATIENT ACCOUNT NUMBER","ClaimStatusCode_02": "1","TotalClaimChargeAmount_03": "34.25","ClaimPaymentAmount_04": "34.25","PatientResponsibilityAmount_05": null,"ClaimFilingIndicatorCode_06": "MC","PayerClaimControlNumber_07": "1000210000000030","FacilityTypeCode_08": "11","ClaimFrequencyCode_09": null,"PatientStatusCode_10": null,"DiagnosisRelatedGroupDRGCode_11": null,"DiagnosisRelatedGroupDRGWeight_12": null,"DischargeFraction_13": null,"YesNoConditionorResponseCode_14": null
                        },"CAS_ClaimsAdjustment": null,"AllNM1": {
                            "NM1_PatientName": {
                                "EntityIdentifierCode_01": "QC","EntityTypeQualifier_02": "1","ResponseContactLastorOrganizationName_03": "SUBMITTED LAST","ResponseContactFirstName_04": "SUBMITTED FIRST","ResponseContactMiddleName_05": null,"NamePrefix_06": null,"ResponseContactNameSuffix_07": null,"IdentificationCodeQualifier_08": "MI","ResponseContactIdentifier_09": "LL99999L","EntityRelationshipCode_10": null,"EntityIdentifierCode_11": null,"NameLastorOrganizationName_12": null
                            },"NM1_InsuredName": null,"NM1_CorrectedPatient_InsuredName": {
                                "EntityIdentifierCode_01": "74","ResponseContactLastorOrganizationName_03": "CORRECTED LAST","ResponseContactFirstName_04": "CORRECTED FIRST","IdentificationCodeQualifier_08": null,"ResponseContactIdentifier_09": null,"NM1_ServiceProviderName": null,"NM1_CrossoverCarrierName": null,"NM1_CorrectedPriorityPayerName": null,"NM1_OtherSubscriberName": null
                        },"MIA_InpatientAdjudicationInformation": null,"MOA_OutpatientAdjudicationInformation": null,"AllREF": {
                            "REF_OtherClaimRelatedIdentification": [
                                {
                                    "ReferenceIdentificationQualifier_01": "EA","MemberGrouporPolicyNumber_02": "PATIENT ACCOUNT NUMBER","ReferenceIdentifier_04": null
                                }
                            ],"REF_RenderingProviderIdentification": null
                        },"AllDTM": {
                            "DTM_StatementFromorToDate": [
                                {
                                    "DateTimeQualifier_01": "232","DateTimePeriod_06": null
                                },{
                                    "DateTimeQualifier_01": "233","DateTimePeriod_06": null
                                }
                            ],"DTM_CoverageExpirationDate": null,"DTM_ClaimReceivedDate": null
                        },"PER_ClaimContactInformation": null,"AMT_ClaimSupplementalInformation": [
                            {
                                "AmountQualifierCode_01": "AU","TotalClaimChargeAmount_02": "34.25","CreditDebitFlagCode_03": null
                            }
                        ],"QTY_ClaimSupplementalInformationQuantity": null,"Loop2110": null
                    },{
                        "CLP_ClaimPaymentInformation": {
                            "PatientControlNumber_01": "PATIENT ACCOUNT NUMBER","ClaimStatusCode_02": "2","TotalClaimChargeAmount_03": "34","ClaimPaymentAmount_04": "0","PayerClaimControlNumber_07": "1000220000000020","ResponseContactIdentifier_09": "LL88888L","AMT_ClaimSupplementalInformation": null,"ClaimPaymentAmount_04": "11.5","PayerClaimControlNumber_07": "1000230000000020","ResponseContactIdentifier_09": "LL77777L","TotalClaimChargeAmount_02": "11.5","Loop2110": null
                    }
                ]
            }
        ],"PLB_ProviderAdjustment": null,"SE": {
            "NumberofIncludedSegments_01": "65","TransactionSetControlNumber_02": "1740"
        },"ErrorContext": {
            "Name": "835","ControlNumber": "1740","Edition": "005010","Release": "X221A1","Index": 2,"ValidatedSegmentsCount": 0,"Codes": [],"Errors": [
                {
                    "Name": "SVC","Position": 20,"LoopId": null,"Value": "SVC*HC:V2020:RB*6*6**1","Codes": [
                        4
                    ],"Errors": [],"Message": "Segment SVC*HC:V2020:RB*6 is not allowed in this position. Only the following segments can appear: AMT,QTY,SVC,CLP,LX,PLB,SE"
                }
            ],"HasErrors": true,"Message": null
        }
    }

可以将此字符串粘贴到在线 JSON 解析器中以更轻松地可视化层次结构(如 http://json.parser.online.fr/)。

我想要做的是使用 SQL Server OPENJSON 函数来获取此 JSON 字符串中 3 个示例声明的结果集。声明嵌套在 $.Loop2000.Loop2100 中,我试图得到的结果是:

    Patient Account Number   ClaimStatusCode     TotalClaimChargeAmount
   ------------------------------------------------------------------------
    Patient Account Number   1                   34.25
    
    Patient Account Number   2                   34.00
    
    Patient Account Number   3                   34.00

我似乎无法在 OPENJSON 函数中找到正确的路径以将其转换为具有 3 行的结果集。我正在尝试使用类似的东西:

SELECT * FROM  OPENJSON(@sJSON,N'$.Loop2000.Loop2100')  
WITH (  
    PatientControlNumber_01 nvarchar(100) '$.CLP_ClaimPaymentInformation.PatientControlNumber_01'
)  

任何帮助将不胜感激。

解决方法

您可以使用的一种方法是一些嵌套的 OPNEJSON 调用:

SELECT CPI.*
FROM OPENJSON(@JSON,N'$.Loop2000') 
     WITH (Loop2100 nvarchar(MAX) AS JSON) L2000
     CROSS APPLY OPENJSON(L2000.Loop2100)
                 WITH(CLP_ClaimPaymentInformation nvarchar(MAX) AS JSON) L2100
     CROSS APPLY OPENJSON(L2100.CLP_ClaimPaymentInformation)
                 WITH (PatientControlNumber varchar(100) '$.PatientControlNumber_01',ClaimStatusCode int '$.ClaimStatusCode_02',TotalClaimChargeAmount decimal(12,2) '$.TotalClaimChargeAmount_03') CPI;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-