简体   繁体   中英

Trying to build complex nested JSON using JOLT

I am trying to build complex nested array of JSON objects. I am struggling to get my expected structure using JOLT. Any help would be appreciated.

I am trying to understand JOLT from the appspot and it is hard for me to grasp about recursively iterating over data set.

  • The "JobId" should be same to milliseconds even if there are 1000 objects in array that is why i am generating "JobId" one time rather than in every object and try to move to right place in next iteration unsuccessfully.

The Input JSON is:

{
  "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": "-"
      }
    }
    ]
}

The Spec I came up with so far:

[{
    "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"
          }]
        }
      }
    }
    }

]

The out put with the spec is:

{
  "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()}"
}

Expected output is:

{
  "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"
    } ]
  } ],

}

This is the spec That gave me what i am looking for:

 [{
    "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"
            }
          }
        }
      }
    }
    }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM