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

尝试使用JOLT构建复杂的嵌套JSON

廖招
2023-03-14

我正在尝试构建复杂的JSON对象嵌套数组。我正在努力使用JOLT获得我期望的结构。任何帮助都将不胜感激。

我正试图从appspot上理解JOLT,但对于我来说,很难理解对数据集进行递归迭代。

  • 即使数组中有 1000 个对象,“JobId”也应该与毫秒相同,这就是为什么我一次生成“JobId”而不是在每个对象中并尝试在下一次迭代中移动到正确的位置失败的原因。

输入的 JSON 为:

{
  "type": "FeatureCollection",
  "totalFeatures": 2,
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          13.429916995511855,
          52.54516530881942
        ]
      },
      "properties": {
        "feature_type": "BLDG",
        "feature_geometry": "point",
        "discipline": "LOC",
        "activity": "AC",
        "be_number": 12313,
        "category": 47400,
        "condition": "RDY",
        "review_date": "2018-03-28T21:36:32.325Z",
        "role_type": "INSG",
        "symbol_code": "-"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          16.429916995511855,
          91.54516530881942
        ]
      },
      "properties": {
        "feature_type": "Bridge",
        "feature_geometry": "point",
        "discipline": "LOC",
        "activity": "AC",
        "co_number": 12313,
        "category": 47400,
        "condition": "RDY",
        "review_date": "2018-03-28T21:36:32.325Z",
        "role_type": "KLDV",
        "symbol_code": "-"
      }
    }
    ]
}

到目前为止,我想出的规范:

[{
    "operation": "shift",
    "spec": {
      "type": "type",
      "features": {
        "*": {
          "geometry": {
            "coordinates": {
              "0": "FacilityComposite[&3].Facility.Ilat",
              "1": "FacilityComposite[&3].Facility.Ilon"
            }
          },
          "properties": {
            "activity": "FacilityComposite[&2].Facility.Activity",
            "be_number": "FacilityComposite[&2].Facility.BeNumber",
            "category": "FacilityComposite[&2].Facility.Category",
            "condition": "FacilityComposite[&2].Facility.Condition",
            "role_type": ["FacilityComposite[&2].Facility.RoleType", "FacilityComposite[&2].FacForm.RoleType", "FacilityComposite[&2].FacTie[&2].RoleType"],
            "review_date": ["FacilityComposite[&2].Facility.ReviewDate", "FacilityComposite[&2].FacForm.ReviewDate", "FacilityComposite[&2].FacTie[&2].ReviewDate"]
          }
        }
      }
    }
    }, {
    "operation": "default",
    "spec": {
      "JobId": "${now():toNumber()}",
      "FacilityComposite[]": {
        "*": {
          "Facility": {
            "FacSk": "-1",
            "ClientId": "DISHNET",
            "ReviewDate": "${now()}"
          },
          "FacForm": {
            "FacSk": "-1",
            "ClientId": "DISHNET"
          },
          "FacTie": [{
            "FacTieSk": "-1",
            "TieFromSk": "-1",
            "TieToSk": "-1",
            "ClientId": "DISHNET"
          }]
        }
      }
    }
    }

]

规范的输出是:

{
  "type" : "FeatureCollection",
  "FacilityComposite" : [ {
    "Facility" : {
      "Ilat" : 13.429916995511855,
      "Ilon" : 52.54516530881942,
      "Activity" : "AC",
      "BeNumber" : 12313,
      "Category" : 47400,
      "Condition" : "RDY",
      "RoleType" : "INSG",
      "ReviewDate" : "2018-03-28T21:36:32.325Z",
      "ClientId" : "DISHNET",
      "FacSk" : "-1"
    },
    "FacForm" : {
      "RoleType" : "INSG",
      "ReviewDate" : "2018-03-28T21:36:32.325Z",
      "FacSk" : "-1",
      "ClientId" : "DISHNET"
    },
    "FacTie" : [ {
      "RoleType" : "INSG",
      "ReviewDate" : "2018-03-28T21:36:32.325Z"
    } ]
  }, {
    "Facility" : {
      "Ilat" : 16.429916995511856,
      "Ilon" : 91.54516530881942,
      "Activity" : "AC",
      "Category" : 47400,
      "Condition" : "RDY",
      "RoleType" : "KLDV",
      "ReviewDate" : "2018-03-28T21:36:32.325Z",
      "ClientId" : "DISHNET",
      "FacSk" : "-1"
    },
    "FacForm" : {
      "RoleType" : "KLDV",
      "ReviewDate" : "2018-03-28T21:36:32.325Z",
      "FacSk" : "-1",
      "ClientId" : "DISHNET"
    },
    "FacTie" : [ null, {
      "RoleType" : "KLDV",
      "ReviewDate" : "2018-03-28T21:36:32.325Z"
    } ]
  } ],
  "JobId" : "${now():toNumber()}"
}

预期输出为:

{
  "type" : "FeatureCollection",
  [ 
  "FacilityComposite" : {
    "Facility" : {
      "JobId" : "${now():toNumber()}"
      "Ilat" : 13.429916995511855,
      "Ilon" : 52.54516530881942,
      "Activity" : "AC",
      "BeNumber" : 12313,
      "Category" : 47400,
      "Condition" : "RDY",
      "RoleType" : "INSG",
      "ReviewDate" : "2018-03-28T21:36:32.325Z",
      "ClientId" : "DISHNET",
      "FacSk" : "-1"
    },
    "FacForm" : {
      "JobId" : "${now():toNumber()}"
      "RoleType" : "INSG",
      "ReviewDate" : "2018-03-28T21:36:32.325Z",
      "FacSk" : "-1",
      "ClientId" : "DISHNET"
    },
    "FacTie" : [ {
    "FacTieSk": "-1",
        "TieFromSk": "-1",
        "TieToSk": "-1",
        "JobId" : "${now():toNumber()}"
        "ClientId": "DISHNET"
        "RoleType" : "INSG",
        "ReviewDate" : "2018-03-28T21:36:32.325Z"
    } ]
  },  
  "FacilityComposite" :{    
    "Facility" : {
      "JobId" : "${now():toNumber()}"
      "Ilat" : 16.429916995511856,
      "Ilon" : 91.54516530881942,
      "Activity" : "AC",
      "Category" : 47400,
      "Condition" : "RDY",
      "RoleType" : "KLDV",
      "ReviewDate" : "2018-03-28T21:36:32.325Z",
      "ClientId" : "DISHNET",
      "FacSk" : "-1"
    },
    "FacForm" : {
      "JobId" : "${now():toNumber()}"
      "RoleType" : "KLDV",
      "ReviewDate" : "2018-03-28T21:36:32.325Z",
      "FacSk" : "-1",
      "ClientId" : "DISHNET"
    },
    "FacTie" : [ {
      "TieFromSk": "-1",
        "TieToSk": "-1",
        "JobId" : "${now():toNumber()}"
        "ClientId": "DISHNET"
        "RoleType" : "INSG",
        "ReviewDate" : "2018-03-28T21:36:32.325Z"
    } ]
  } ],

}

共有1个答案

何向荣
2023-03-14

这是给了我正在寻找的东西的规范:

 [{
    "operation": "shift",
    "spec": {
      "type": "type",
      "features": {
        "*": {
          "geometry": {
            "coordinates": {
              "0": "FacilityComposite[&3].Facility.Ilat",
              "1": "FacilityComposite[&3].Facility.Ilon"
            }
          },
          "properties": {
            "activity": "FacilityComposite[&2].Facility.Activity",
            "be_number": "FacilityComposite[&2].Facility.BeNumber",
            "category": "FacilityComposite[&2].Facility.Category",
            "condition": "FacilityComposite[&2].Facility.Condition",
            "role_type": ["FacilityComposite[&2].Facility.RoleType", "FacilityComposite[&2].FacForm.RoleType", "FacilityComposite[&2].FacTie[&2].RoleType"],
            "review_date": ["FacilityComposite[&2].Facility.ReviewDate", "FacilityComposite[&2].FacForm.ReviewDate", "FacilityComposite[&2].FacTie[&2].ReviewDate"]
          }
        }
      }
    }
    }, {
    "operation": "default",
    "spec": {
      "JobId": "${now():toNumber()}",
      "FacilityComposite[]": {
        "*": {
          "Facility": {
            "FacSk": "-1",
            "ClientId": "DISHNET",
            "ReviewDate": "${now()}"
          },
          "FacForm": {
            "FacSk": "-1",
            "ClientId": "DISHNET"
          }
        }
      }
    }
    },
  {
    "operation": "modify-default-beta",
    "spec": {
      "FacilityComposite": {
        "*": {
          "FacTie": {
            "*": {
              "FacTieSk": "-1",
              "TieFromSk": "-1",
              "TieToSk": "-1",
              "ClientId": "DISHNET"
            }
          }
        }
      }
    }
    }
]
 类似资料:
  • 我试图根据第二个嵌套数组中的值的数量将嵌套数组转换为对象。我似乎无法获取值字段的数量并将其用作规范中的键。现在这是我的输入JSON文件: 这是我想要的JSON输出: 这是我目前的规格 有人有类似的情况吗?

  • 我有一个这样的数组: 中的数组包括另外两个数组(第一个不是必需的,但看看第二个(:这个数组包含不同的 x/y 坐标 ) 我想得到另一个数组的结果,如下所示(解释如下): 数组现在按其 x 值排序 ( -- 我不知道如何编码;这是我到目前为止所拥有的: 编辑:有一点忘记说了,就是应该分组的坐标的不应该大于。看下面的例子:

  • 我有一个复杂的Json,我想使用Jolt或任何其他方式在Nifi中展平Json。也可以。任何人都可以帮助展平下面的Json吗 以上是我的Json,我需要将其扁平化为简单的Json,以便我可以在Hive中处理它。我曾尝试使用flatten Json处理器,但它不起作用,所以现在我正在尝试使用jolt规范。请任何人使用jolt转换或在Nifi中指导我解决上述问题。 我的预期输出如下 我已经更新了请求

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

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

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