首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nifi中使用jolt表达式转换复杂嵌套的json数组

在nifi中使用jolt表达式转换复杂嵌套的json数组
EN

Stack Overflow用户
提问于 2022-02-08 18:12:47
回答 3查看 213关注 0票数 0

我有一个复杂的Json,我想用抖动或任何其他方法使Json在Nifi变平也很好,谁能帮我把下面的JSON压平吗

代码语言:javascript
复制
{
  "veread": "ndjdjjywuieopppqpppwaghhzuii",
  "debittanceInformation": [
    {
      "internalDocumentInformation": [
        {
          "number": "8",
          "lineDetails": [
            {
              "identifications": [
                {
                  "number": "44",
                  "model": {
                    "code": "xdd",
                    "proprietary": "ddy"
                  },
                  "relatedDate": "3/4"
                }
              ],
              "quantity": 136,
              "unitOfMeasure": "each",
              "money": {
                "duePayablemoney": 44,
                "discountAppliedmoneys": [
                  {
                    "model": {
                      "code": "dddddd",
                      "proprietary": "rddddy"
                    },
                    "money": 216.2
                  }
                ],
                "valuemoneys": [
                  {
                    "model": {
                      "code": "ddddd",
                      "proprietary": "dddd"
                    },
                    "money": 391.41
                  }
                ],
                "adjustmentmoneyAndReasons": [
                  {
                    "money": 824.54,
                    "reason": "dd"
                  }
                ],
                "debittedmoney": 660.7,
                "creditNotemoney": 985.63
              }
            }
          ],
          "internalDocumentmoney": {
            "duePayablemoney": 567.45,
            "discountAppliedmoneys": [
              {
                "model": {
                  "code": "DD",
                  "proprietary": "EE"
                },
                "money": 5.64
              }
            ],
            "valuemoneys": [
              {
                "model": {
                  "code": "z",
                  "proprietary": "a"
                },
                "money": 145
              }
            ],
            "adjustmentmoneyAndReasons": [
              {
                "money": 678,
                "reason": "tyuiop"
              }
            ],
            "debittedmoney": 123,
            "creditNotemoney": 456
          }
        }
      ],
      "supplierReferenceInformation": {
        "model": {
          "code": "x",
          "proprietary": "y"
        },
        "issuer": "issuer",
        "reference": "hfhjfdozkdfl;fdko;k"
      },
      "billr": {
        "name": "jdjaiofjiosJOfjaiodjoiwaj",
        "companyIdentifications": [
          {
            "identification": "74747435438",
            "model": "jdidfjdsjf"
          }
        ]
      },
      "bille": {
        "name": "djhsaijdwjadfja;dddF",
        "companyIdentifications": [
          {
            "identification": "dddddd",
            "model": "kksjdadawx"
          }
        ]
      },
      "additionaldebittanceInformation": [
        "dd",
        "xnjasndfjafnlkNDflkhlfkacmakjhfncasklnf"
      ]
    }
  ],
  "chargesInformation": []
}

上面是我的Json,我需要把它压缩成简单的JSON,这样我就可以在Hive中处理它了。我试过使用扁平的JSON处理器,但它不起作用,所以现在我尝试使用JSON规范,请任何一个人都可以使用JSON转换或在Nifi指导我解决上面的问题。

我的预期产出如下

代码语言:javascript
复制
{
    "veread": "ndjdjjywuieopppqpppwaghhzuii",
    "ddddwww_internalDocumentInformation_number": "8",
    "ddddwww_internalDocumentInformation_lineDetails_identifications_number": "1025",
    "ddddwww_internalDocumentInformation_lineDetails_identifications_model.code": "x",
    "ddddwww_internalDocumentInformation_lineDetails_identifications_model.proprietary": "y",
    "ddddwww_internalDocumentInformation_lineDetails_identifications_relatedDate": "3/23",
    "ddddwww_internalDocumentInformation_lineDetails_quantity": 136,
    "ddddwww_internalDocumentInformation_lineDetails_unitOfMeasure": "each",
    "ddddwww_internalDocumentInformation_lineDetails_money.duePayablemoney": 957.86,
    "ddddwww_internalDocumentInformation_lineDetails_money.discountAppliedmoneys_model.code": "dvbnqwe",
    "ddddwww_internalDocumentInformation_lineDetails_money.discountAppliedmoneys_model.proprietary": "rqazloy",
    "ddddwww_internalDocumentInformation_lineDetails_money.discountAppliedmoneys_money": 216.2,
    "ddddwww_internalDocumentInformation_lineDetails_money.valuemoneys_model.code": "YUIO",
    "ddddwww_internalDocumentInformation_lineDetails_money.valuemoneys_model.proprietary": "BHRTY",
    "ddddwww_internalDocumentInformation_lineDetails_money.valuemoneys_money": 391.41,
    "ddddwww_internalDocumentInformation_lineDetails_money.adjustmentmoneyAndReasons_money": 824.54,
    "ddddwww_internalDocumentInformation_lineDetails_money.adjustmentmoneyAndReasons_reason": "BONUS",
    "ddddwww_internalDocumentInformation_lineDetails_money.debittedmoney": 660.7,
    "ddddwww_internalDocumentInformation_lineDetails_money.creditNotemoney": 985.63,
    "ddddwww_internalDocumentInformation_internalDocumentmoney.duePayablemoney": 567.45,
    "ddddwww_internalDocumentInformation_internalDocumentmoney.discountAppliedmoneys_model.code": "DD",
    "ddddwww_internalDocumentInformation_internalDocumentmoney.discountAppliedmoneys_model.proprietary": "EE",
    "ddddwww_internalDocumentInformation_internalDocumentmoney.discountAppliedmoneys_money": 5.64,
    "ddddwww_internalDocumentInformation_internalDocumentmoney.valuemoneys_model.code": "z",
    "ddddwww_internalDocumentInformation_internalDocumentmoney.valuemoneys_model.proprietary": "a",
    "ddddwww_internalDocumentInformation_internalDocumentmoney.valuemoneys_money": 145,
    "ddddwww_internalDocumentInformation_internalDocumentmoney.adjustmentmoneyAndReasons_money": 678,
    "ddddwww_internalDocumentInformation_internalDocumentmoney.adjustmentmoneyAndReasons_reason": "tyuiop",
    "ddddwww_internalDocumentInformation_internalDocumentmoney.debittedmoney": 123,
    "ddddwww_internalDocumentInformation_internalDocumentmoney.creditNotemoney": 456,
    "ddddwww_supplierReferenceInformation.model.code": "x",
    "ddddwww_supplierReferenceInformation.model.proprietary": "y",
    "ddddwww_supplierReferenceInformation.issuer": "issuer",
    "ddddwww_supplierReferenceInformation.reference": "hfhjfdozkdfl;fdko;k",
    "ddddwww_billr.name": "jdjaiofjiosJOfjaiodjoiwaj",
    "ddddwww_billr.companyIdentifications_identification": "74747435438",
    "ddddwww_billr.companyIdentifications_model": "jdidfjdsjf",
    "ddddwww_bille.name": "djhsaijdwjadfja;lMEjknEAKDLJOJRF",
    "ddddwww_bille.companyIdentifications_identification": "mkskfsajdwiojdijRUWa",
    "ddddwww_bille.companyIdentifications_model": "kksjdadawx",
    "ddddwww_additionalddddwww[0]": "JDSJAHDJKLJHRFQWJESJqiorwjd",
    "ddddwww_additionalddddwww[1]": "xnjasndfjafnlkNDflkhlfkacmakjhfncasklnf",
    "chargesInformation": []
}

我已经更新了请求

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-02-09 10:39:58

使用FlattenJson处理器和属性:

  • Separator_
  • Flatten Modenormal

产出json:

代码语言:javascript
复制
{
  "transactionId" : "ndjdjjywuieopppqpppwaghhzuii",
  "debittanceInformation[0]_internalDocumentInformation[0]_number" : "8",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_identifications[0]_number" : "1025",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_identifications[0]_model_code" : "x",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_identifications[0]_model_proprietary" : "y",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_identifications[0]_relatedDate" : "3\/23",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_quantity" : 136,
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_unitOfMeasure" : "each",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_duePayablemoney" : 957.86,
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_discountAppliedmoneys[0]_model_code" : "dvbnqwe",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_discountAppliedmoneys[0]_model_proprietary" : "rqazloy",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_discountAppliedmoneys[0]_money" : 216.2,
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_valuemoneys[0]_model_code" : "YUIO",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_valuemoneys[0]_model_proprietary" : "BHRTY",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_valuemoneys[0]_money" : 391.41,
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_adjustmentmoneyAndReasons[0]_money" : 824.54,
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_adjustmentmoneyAndReasons[0]_reason" : "BONUS",
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_debittedmoney" : 660.7,
  "debittanceInformation[0]_internalDocumentInformation[0]_lineDetails[0]_money_creditNotemoney" : 985.63,
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_duePayablemoney" : 567.45,
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_discountAppliedmoneys[0]_model_code" : "DD",
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_discountAppliedmoneys[0]_model_proprietary" : "EE",
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_discountAppliedmoneys[0]_money" : 5.64,
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_valuemoneys[0]_model_code" : "z",
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_valuemoneys[0]_model_proprietary" : "a",
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_valuemoneys[0]_money" : 145,
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_adjustmentmoneyAndReasons[0]_money" : 678,
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_adjustmentmoneyAndReasons[0]_reason" : "tyuiop",
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_debittedmoney" : 123,
  "debittanceInformation[0]_internalDocumentInformation[0]_internalDocumentmoney_creditNotemoney" : 456,
  "debittanceInformation[0]_supplierReferenceInformation_model_code" : "x",
  "debittanceInformation[0]_supplierReferenceInformation_model_proprietary" : "y",
  "debittanceInformation[0]_supplierReferenceInformation_issuer" : "issuer",
  "debittanceInformation[0]_supplierReferenceInformation_reference" : "hfhjfdozkdfl;fdko;k",
  "debittanceInformation[0]_billr_name" : "jdjaiofjiosJOfjaiodjoiwaj",
  "debittanceInformation[0]_billr_companyIdentifications[0]_identification" : "74747435438",
  "debittanceInformation[0]_billr_companyIdentifications[0]_model" : "jdidfjdsjf",
  "debittanceInformation[0]_bille_name" : "djhsaijdwjadfja;lMEjknEAKDLJOJRF",
  "debittanceInformation[0]_bille_companyIdentifications[0]_identification" : "mkskfsajdwiojdijRUWa",
  "debittanceInformation[0]_bille_companyIdentifications[0]_model" : "kksjdadawx",
  "debittanceInformation[0]_additionaldebittanceInformation[0]" : "JDSJAHDJKLJHRFQWJESJqiorwjd",
  "debittanceInformation[0]_additionaldebittanceInformation[1]" : "xnjasndfjafnlkNDflkhlfkacmakjhfncasklnf",
  "chargesInformation" : [  ]
}
票数 1
EN

Stack Overflow用户

发布于 2022-02-09 19:19:34

您可以在JoltTransformJSON处理器中使用下面的shift转换

代码语言:javascript
复制
[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "debittanceInformation": {
        "*": {
          "internalDocumentInformation": {
            "*": {
              "number": "&4_&2_&",
              "lineDetails": {
                "*": {
                  "identifications": {
                    "*": {
                      "*": {
                        "@": "&9_&7_&5_&3_&"
                      },
                      "model": {
                        "*": {
                          "@": "&10_&8_&6_&4_&2\\.&"
                        }
                      }
                    }
                  },
                  "quantity": "&6_&4_&2_&",
                  "unitOfMeasure": "&6_&4_&2_&",
                  "money": {
                    "*": {
                      "@": "&8_&6_&4_&2\\.&"
                    },
                    "discountAppliedmoneys": {
                      "*": {
                        "model": {
                          "*": {
                            "@": "&11_&9_&7_&5\\.&4_&2\\.&"
                          }
                        },
                        "*": "&9_&7_&5_&3\\.&2_&"
                      }
                    },
                    "valuemoneys": {
                      "*": {
                        "model": {
                          "*": {
                            "@": "&11_&9_&7_&5\\.&4_&2\\.&"
                          }
                        },
                        "*": "&9_&7_&5_&3\\.&2_&"
                      }
                    },
                    "adjustmentmoneyAndReasons": {
                      "*": {
                        "*": "&9_&7_&5_&3\\.&2_&"
                      }
                    }
                  }
                }
              },
              "internalDocumentmoney": {
                "*": {
                  "@": "&6_&4_&2\\.&"
                },
                "discountAppliedmoneys": {
                  "*": {
                    "model": {
                      "*": "&8_&6_&4_&3_&1\\.&"
                    },
                    "*": "&7_&5_&3\\.&2_&"
                  }
                },
                "valuemoneys": {
                  "*": {
                    "model": {
                      "*": "&8_&6_&4\\.&3_&1\\.&"
                    },
                    "*": "&7_&5_&3\\.&2_&"
                  }
                },
                "adjustmentmoneyAndReasons": {
                  "*": {
                    "*": "&7_&5_&3\\.&2_&"
                  }
                }
              }
            }
          },
          "supplierReferenceInformation": {
            "model": {
              "*": "&4_&2\\.&"
            },
            "*": "&3_&1\\.&"
          },
          "bill*": {
            "companyIdentifications": {
              "*": {
                "*": "&5_&3\\.&2_&"
              }
            },
            "*": "&3_&1\\.&"
          },
          "additionaldebittanceInformation": {
            "*": "&3_&1\\[&\\]"
          }
        }
      }
    }
  }
]
票数 1
EN

Stack Overflow用户

发布于 2022-04-26 02:13:21

将不确定级别的JSON记录扁平化为二维表的Java代码非常长。

您可以使用SPL,一个开放源代码的Java包来完成这个任务。它很方便,四行代码就足够了:img

SPL提供了由Java调用的JDBC驱动程序。只需将上面的SPL脚本存储为flattenjson.splx,并在调用存储过程时在Java中调用它:

代码语言:javascript
复制
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call flattenjson()");
st.execute();
…
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71038813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档