如何解析雪花中的嵌套JSON并提取特定信息?

时间:2020-08-21 19:47:14

标签: json multidimensional-array snowflake-cloud-data-platform

我对雪花非常陌生,以前曾在Teradata SQL中工作。我正在尝试从雪花表中的列中检索信息,该表的值在JSON中,如下所示:

{
  "depositsDMPResponseVO": {
    "depositsApplicationRsnsVO": {
      "applicationID": "YYYYYYY",
      "applicationRefNbr": "XXXXXXXXX",
      "depositsStrategyOutputVOs": [
        {
          "appDescnTms": "2020-08-18T08:07:00Z",
          "depositsAppDecisionVO": {
            "decision": "RV",
            "decisionType": "FRD",
            "finalDecision": false,
            "reasonCodes": [
              "DO6001"
            ],
            "rule": false,
            "ruleId": "R1002",
            "ruleName": "WebChk NewCust TotRsk",
            "strategyName": "Web Checking Application Review",
            "weight": 2705
          },
          "depositsApplicantDescnVOs": [
            {
              "applicantDecisionsRuleVOs": [
                {
                  "decision": "AP",
                  "decisionType": "CIP",
                  "finalDecision": false,
                  "reasonCodes": [],
                  "rule": true,
                  "ruleName": "DefaultCIP",
                  "strategyName": "DefaultCIP",
                  "weight": 100
                },
                {
                  "decision": "RV",
                  "decisionType": "FRD",
                  "finalDecision": false,
                  "reasonCodes": [
                    "DO6001"
                  ],
                  "rule": true,
                  "ruleId": "R1002",
                  "ruleName": "WebChk NewCust TotRsk",
                  "strategyName": "Web Checking Application Review",
                  "weight": 2705
                },
                {
                  "decision": "AP",
                  "decisionType": "CIP",
                  "finalDecision": false,
                  "reasonCodes": [],
                  "rule": false,
                  "ruleName": "DefaultCIP",
                  "strategyName": "DefaultCIP",
                  "weight": 100
                },
                {
                  "decision": "RV",
                  "decisionType": "FRD",
                  "finalDecision": false,
                  "reasonCodes": [
                    "DO6001"
                  ],
                  "rule": false,
                  "ruleId": "R1002",
                  "ruleName": "WebChk NewCust TotRsk",
                  "strategyName": "Web Checking Application Review",
                  "weight": 2705
                },
                {
                  "decision": "RV",
                  "decisionType": "FRD",
                  "finalDecision": true,
                  "reasonCodes": [
                    "DO6001"
                  ],
                  "rule": false,
                  "ruleId": "R1002",
                  "ruleName": "WebChk NewCust TotRsk",
                  "strategyName": "Web Checking Application Review",
                  "weight": 2705
                }
              ],
              "applicantModelScoreVOs": [
                {
                  "modelId": "CHEX",
                  "reasonCd": [
                    "AX",
                    "AP",
                    "AD",
                    "AB",
                    null
                  ],
                  "scoreNumber": "0576"
                }
              ],
              "applicantNbr": "DBAPPLCNT5920115",
              "cipPrimVerifySrc": "LNIID",
              "cipVerifyBy": "LexisNexis",
              "cipVerifyMode": "NDOC",
              "strDrvdAttr01": "isMktSrcHghRsk : 1",
              "strDrvdAttr02": "totRskVal : 11",
              "strDrvdAttr03": "isBnkCust : 0",
              "strDrvdAttr04": "bnkCustTen : 0",
              "strDrvdAttr05": "hasChkPrdct : 1",
              "strDrvdAttr06": "isCrdCust : 0",
              "strDrvdAttr07": "newTMXRskVal : 5",
              "strDrvdAttr08": "TMXRskVal : 4",
              "strDrvdAttr09": "newLNFPRskVal : 2",
              "strDrvdAttr10": "LNFPRskVal : 1",
              "strDrvdAttr11": "newEMAGRskVal : 4",
              "strDrvdAttr12": "EMAGRskVal : 4",
              "strDrvdAttr13": "tenureRskVal : 3",
              "strDrvdAttr14": "riskyMrktSrcInd : 1",
              "strDrvdAttr15": "jntOwnInd : 0",
              "strDrvdAttr16": "expL3CallInd : 0",
              "strDrvdAttr17": "lnsFPCallInd : 0",
              "strDrvdAttr18": "hasCDorIRA : 0"
            }
          ],
          "strategyType": "DepositDM"
        }
      ]
    }
  }
}

我只对提取键“ strDrvdAttr02”,“ strDrvdAttr04”的值感兴趣。我以前使用flatten来获得更简单的JSON,但这对我来说太复杂了。我试图像下面这样嵌套扁平化,但是认为这行不通:

   WITH DATA2 AS 
   (
                WITH DATA1 AS  
                (
                SELECT
                APLN_ID
                ,parse_json("DMP_OUTP_VAL") AS json
                FROM "SFAAP"."V_SOT_DIRBNK_CRSPRD_FRD_ALL"."VC_EDP_DECSN_LOG"
                WHERE "DMP_CALL_TYP_CDE"='DEPOSITDM'
                AND APLN_ID = 'XXXXXXXXX'
                )
                SELECT 
                APLN_ID
                ,parse_json(f.value) AS mson
                FROM DATA1 d
                ,lateral flatten(input=>d.json) f
   )
   SELECT
   APLN_ID
   ,f.value
   FROM DATA2 d
   ,lateral flatten(input=>d.mson) f 

我想不出任何更简单的方法来提取这2个属性,它们位于更深层的嵌套JSON中。因此,为什么要寻求专家的帮助。任何评论/帮助,不胜感激。谢谢。

2 个答案:

答案 0 :(得分:0)

请尝试

select value:depositsStrategyOutputVOs[0]:depositsApplicantDescnVOs[0]:strDrvdAttr02 , value:depositsStrategyOutputVOs[0]:depositsApplicantDescnVOs[0]:strDrvdAttr04 from( from(
select parse_json('{
  "depositsDMPResponseVO": {
    "depositsApplicationRsnsVO": {
      "applicationID": "YYYYYYY",
      "applicationRefNbr": "XXXXXXXXX",
      "depositsStrategyOutputVOs": [
        {
          "appDescnTms": "2020-08-18T08:07:00Z",
          "depositsAppDecisionVO": {
            "decision": "RV",
            "decisionType": "FRD",
            "finalDecision": false,
            "reasonCodes": [
              "DO6001"
            ],
            "rule": false,
            "ruleId": "R1002",
            "ruleName": "WebChk NewCust TotRsk",
            "strategyName": "Web Checking Application Review",
            "weight": 2705
          },
          "depositsApplicantDescnVOs": [
            {
              "applicantDecisionsRuleVOs": [
                {
                  "decision": "AP",
                  "decisionType": "CIP",
                  "finalDecision": false,
                  "reasonCodes": [],
                  "rule": true,
                  "ruleName": "DefaultCIP",
                  "strategyName": "DefaultCIP",
                  "weight": 100
                },
                {
                  "decision": "RV",
                  "decisionType": "FRD",
                  "finalDecision": false,
                  "reasonCodes": [
                    "DO6001"
                  ],
                  "rule": true,
                  "ruleId": "R1002",
                  "ruleName": "WebChk NewCust TotRsk",
                  "strategyName": "Web Checking Application Review",
                  "weight": 2705
                },
                {
                  "decision": "AP",
                  "decisionType": "CIP",
                  "finalDecision": false,
                  "reasonCodes": [],
                  "rule": false,
                  "ruleName": "DefaultCIP",
                  "strategyName": "DefaultCIP",
                  "weight": 100
                },
                {
                  "decision": "RV",
                  "decisionType": "FRD",
                  "finalDecision": false,
                  "reasonCodes": [
                    "DO6001"
                  ],
                  "rule": false,
                  "ruleId": "R1002",
                  "ruleName": "WebChk NewCust TotRsk",
                  "strategyName": "Web Checking Application Review",
                  "weight": 2705
                },
                {
                  "decision": "RV",
                  "decisionType": "FRD",
                  "finalDecision": true,
                  "reasonCodes": [
                    "DO6001"
                  ],
                  "rule": false,
                  "ruleId": "R1002",
                  "ruleName": "WebChk NewCust TotRsk",
                  "strategyName": "Web Checking Application Review",
                  "weight": 2705
                }
              ],
              "applicantModelScoreVOs": [
                {
                  "modelId": "CHEX",
                  "reasonCd": [
                    "AX",
                    "AP",
                    "AD",
                    "AB",
                    null
                  ],
                  "scoreNumber": "0576"
                }
              ],
              "applicantNbr": "DBAPPLCNT5920115",
              "cipPrimVerifySrc": "LNIID",
              "cipVerifyBy": "LexisNexis",
              "cipVerifyMode": "NDOC",
              "strDrvdAttr01": "isMktSrcHghRsk : 1",
              "strDrvdAttr02": "totRskVal : 11",
              "strDrvdAttr03": "isBnkCust : 0",
              "strDrvdAttr04": "bnkCustTen : 0",
              "strDrvdAttr05": "hasChkPrdct : 1",
              "strDrvdAttr06": "isCrdCust : 0",
              "strDrvdAttr07": "newTMXRskVal : 5",
              "strDrvdAttr08": "TMXRskVal : 4",
              "strDrvdAttr09": "newLNFPRskVal : 2",
              "strDrvdAttr10": "LNFPRskVal : 1",
              "strDrvdAttr11": "newEMAGRskVal : 4",
              "strDrvdAttr12": "EMAGRskVal : 4",
              "strDrvdAttr13": "tenureRskVal : 3",
              "strDrvdAttr14": "riskyMrktSrcInd : 1",
              "strDrvdAttr15": "jntOwnInd : 0",
              "strDrvdAttr16": "expL3CallInd : 0",
              "strDrvdAttr17": "lnsFPCallInd : 0",
              "strDrvdAttr18": "hasCDorIRA : 0"
            }
          ],
          "strategyType": "DepositDM"
        }
      ]
    }
  }
}')) as src,
lateral flatten(input => src.$1:depositsDMPResponseVO);

答案 1 :(得分:0)

不幸的是,您那里有一些不必要的数组,因此您确实需要展平,但是您可以像联接一样进行操作……

WITH x AS (
SELECT parse_json('{
  "depositsDMPResponseVO": {
    "depositsApplicationRsnsVO": {
      "applicationID": "YYYYYYY",
      "applicationRefNbr": "XXXXXXXXX",
      "depositsStrategyOutputVOs": [
        {
          "appDescnTms": "2020-08-18T08:07:00Z",
          "depositsAppDecisionVO": {
            "decision": "RV",
            "decisionType": "FRD",
            "finalDecision": false,
            "reasonCodes": [
              "DO6001"
            ],
            "rule": false,
            "ruleId": "R1002",
            "ruleName": "WebChk NewCust TotRsk",
            "strategyName": "Web Checking Application Review",
            "weight": 2705
          },
          "depositsApplicantDescnVOs": [
            {
              "applicantDecisionsRuleVOs": [
                {
                  "decision": "AP",
                  "decisionType": "CIP",
                  "finalDecision": false,
                  "reasonCodes": [],
                  "rule": true,
                  "ruleName": "DefaultCIP",
                  "strategyName": "DefaultCIP",
                  "weight": 100
                },
                {
                  "decision": "RV",
                  "decisionType": "FRD",
                  "finalDecision": false,
                  "reasonCodes": [
                    "DO6001"
                  ],
                  "rule": true,
                  "ruleId": "R1002",
                  "ruleName": "WebChk NewCust TotRsk",
                  "strategyName": "Web Checking Application Review",
                  "weight": 2705
                },
                {
                  "decision": "AP",
                  "decisionType": "CIP",
                  "finalDecision": false,
                  "reasonCodes": [],
                  "rule": false,
                  "ruleName": "DefaultCIP",
                  "strategyName": "DefaultCIP",
                  "weight": 100
                },
                {
                  "decision": "RV",
                  "decisionType": "FRD",
                  "finalDecision": false,
                  "reasonCodes": [
                    "DO6001"
                  ],
                  "rule": false,
                  "ruleId": "R1002",
                  "ruleName": "WebChk NewCust TotRsk",
                  "strategyName": "Web Checking Application Review",
                  "weight": 2705
                },
                {
                  "decision": "RV",
                  "decisionType": "FRD",
                  "finalDecision": true,
                  "reasonCodes": [
                    "DO6001"
                  ],
                  "rule": false,
                  "ruleId": "R1002",
                  "ruleName": "WebChk NewCust TotRsk",
                  "strategyName": "Web Checking Application Review",
                  "weight": 2705
                }
              ],
              "applicantModelScoreVOs": [
                {
                  "modelId": "CHEX",
                  "reasonCd": [
                    "AX",
                    "AP",
                    "AD",
                    "AB",
                    null
                  ],
                  "scoreNumber": "0576"
                }
              ],
              "applicantNbr": "DBAPPLCNT5920115",
              "cipPrimVerifySrc": "LNIID",
              "cipVerifyBy": "LexisNexis",
              "cipVerifyMode": "NDOC",
              "strDrvdAttr01": "isMktSrcHghRsk : 1",
              "strDrvdAttr02": "totRskVal : 11",
              "strDrvdAttr03": "isBnkCust : 0",
              "strDrvdAttr04": "bnkCustTen : 0",
              "strDrvdAttr05": "hasChkPrdct : 1",
              "strDrvdAttr06": "isCrdCust : 0",
              "strDrvdAttr07": "newTMXRskVal : 5",
              "strDrvdAttr08": "TMXRskVal : 4",
              "strDrvdAttr09": "newLNFPRskVal : 2",
              "strDrvdAttr10": "LNFPRskVal : 1",
              "strDrvdAttr11": "newEMAGRskVal : 4",
              "strDrvdAttr12": "EMAGRskVal : 4",
              "strDrvdAttr13": "tenureRskVal : 3",
              "strDrvdAttr14": "riskyMrktSrcInd : 1",
              "strDrvdAttr15": "jntOwnInd : 0",
              "strDrvdAttr16": "expL3CallInd : 0",
              "strDrvdAttr17": "lnsFPCallInd : 0",
              "strDrvdAttr18": "hasCDorIRA : 0"
            }
          ],
          "strategyType": "DepositDM"
        }
      ]
    }
  }
}') as payload_json)
    select f2.value:strDrvdAttr02::varchar,
           f2.value:strDrvdAttr03::varchar
    from x,
    lateral flatten(input => x.payload_json:depositsDMPResponseVO:depositsApplicationRsnsVO:depositsStrategyOutputVOs) f,
    lateral flatten(input => f.value:depositsApplicantDescnVOs) f2;