当前位置: 首页 > 知识库问答 >
问题:

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

羊舌涵涤
2023-03-14

我有一个复杂的Json,我想使用Jolt或任何其他方式在Nifi中展平Json。也可以。任何人都可以帮助展平下面的Json吗

{
  "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中处理它。我曾尝试使用flatten Json处理器,但它不起作用,所以现在我正在尝试使用jolt规范。请任何人使用jolt转换或在Nifi中指导我解决上述问题。

我的预期输出如下

json prettyprint-override">{
    "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": []
}

我已经更新了请求

共有3个答案

山凌
2023-03-14

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

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

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

…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call flattenjson()");
st.execute();
…
姚正真
2023-03-14

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

[
  {
    "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\\[&\\]"
          }
        }
      }
    }
  }
]
云韬
2023-03-14

使用具有以下属性的FlattenJson处理器:

    < li> 分隔符: _ < li> 展平模式: 正常

输出json:

{
  "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" : [  ]
}
 类似资料:
  • 我试图根据第二个嵌套数组中的值的数量将嵌套数组转换为对象。我似乎无法获取值字段的数量并将其用作规范中的键。现在这是我的输入JSON文件: 这是我想要的JSON输出: 这是我目前的规格 有人有类似的情况吗?

  • 我有一个关于使用jolt将平面json转换成嵌套json的问题。我对jolt很陌生,这是我的意见 我编写了jolt spec,但我没有得到想要的输出 我的预期产出是: 任何震动专家都可以帮助我获得所需的输出。我应该在颠簸中使用多个变换,还是可以在一个震动变压器中获得所需的输出?

  • 如何使用jolt转换将平面JSON转换为嵌套JSON?我对JSON和jolt是新手。 输入: 预期输出: 编辑:我想在< code>SubFunds中添加一个新字段,但是新字段不在JSON文件中,它是计算字段,我可以按原样添加示例吗 并重命名字段:

  • 当调用外部API时,我收到了一个复杂的json结构,我需要根据我们的需要将其转换为简单的json。我发现jolt具有转换json的能力,但无法提出jolt规范。 我的输入Json数组—— 我的输出 Json 结构应该看起来像 - 我尝试了多种jolt规格,但无法得到想要的输出。这种情况下最理想的jolt规格应该是什么?

  • 我有一个JSON如下所示: 为什么在输出中看不到Level1、Level2?请有人帮忙,我想看看在输出和输入太相似了。

  • 我有json,其中包括多个产品,每个产品有多个不同的细节。使用jolt,我只需要输入json中的几个字段,遵循与输入json几乎相同的结构。我成功地迭代了产品,但是当我试图迭代每个产品变体时,我没有得到想要的输出。 输入. json 这里是Spec.json 我想要的预期输出。 我现在得到的实际输出。