Home > Software design >  Transform a JSON array in Jolt into a new JSON object and keep objects separate
Transform a JSON array in Jolt into a new JSON object and keep objects separate

Time:01-07

I have an array of JSON Objects that I need to transform into a new array of JSON Objects that align with my schema. I am new to Jolt and my output is not what I need. I've attempted using the [&1] and @ operators within but I have yet to get my desired output.

My input:

{
  "totalCount": 2,
  "entities": {
    "people": [
      {
        "uniqueIdentifier": "0105",
        "common": {
          "firstName": "Bob",
          "lastName": "Smith",
          "geoLocation": {
            "geometry": {
              "point": {
                "coordinates": [
                  39.93479016,
                  47.21850072
                ]
              }
            }
          },
          "timeOfReporting": "2019-11-18T18:36:10Z"
        },
        "type": "PERSON",
        "parent": {
          "firstName": "Jane",
          "lastName": "Smith"
        }
      },
      {
        "uniqueIdentifier": "0106",
        "common": {
          "firstName": "Joe",
          "lastName": "Green",
          "geoLocation": {
            "geometry": {
              "point": {
                "coordinates": [
                  39.93479016,
                  47.21850072
                ]
              }
            }
          },
          "timeOfReporting": "2019-11-18T18:36:10Z"
        },
        "Type": "PERSON",
        "parent": {
          "firstName": "John",
          "lastName": "Green"
        }
      }
    ]
  }
} 

My Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "entities": {
        "people": {
          "*": {
            "uniqueIdentifier": "Person.Id",
            "type": "Person.Type",
            "parent": {
              "firstName": "Person.Parent.FirstName",
              "lastName": "Person.Parent.LastName"
            },
            "common": {
              "firstName": "Person.FirstName",
              "lastName": "Person.LastName",
              "timeOfReporting": "Person.DateCreated",
              "geoLocation": {
                "geometry": {
                  "point": {
                    "coordinates": {
                      "0": "Person.Locations.Location.Longitude",
                      "1": "Person.Locations.Location.Latitude"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "Person": {
        "Processed": "false"
      }
    }
  }
  ]

Current Output:

{
  "Person" : {
    "DateCreated" : [ "2019-11-18T18:36:10Z", "2019-11-18T18:36:10Z" ],
    "FirstName" : [ "Bob", "Joe" ],
    "Id" : [ "0105", "0106" ],
    "LastName" : [ "Smith", "Green" ],
    "Locations" : {
      "Location" : {
        "Latitude" : [ 47.21850072, 47.21850072 ],
        "Longitude" : [ 39.93479016, 39.93479016 ]
      }
    },
    "Parent" : {
      "FirstName" : [ "Jane", "John" ],
      "LastName" : [ "Smith", "Green" ]
    },
    "Processed" : "false",
    "Type" : "PERSON"
  }
}

Desired Output:

{
    "Person":{
        "DateCreated": "2019-11-18T18:36:10Z",
        "FirstName": "Bob",
        "LastName" "Smith"
        "Id": "0105",
        "Locations": {
            "Location": {
                "Latitude": 47.2184,
                "Longitude": 39.7854
            }
        },
        "Parent": {
            "FirstName": "Jane",
            "LastName": "Smith"
        },
        "Processed": "false",
        "Type": "PERSON"
    },
    "Person": {
        "DateCreated": "2019-11-18T18:36:10Z",
        "FirstName": "Joe",
        "LastName": "Green"
        "Id": "0106",
        "Locations": {
            "Location": {
                "Latitude": 42.2184,
                "Longitude": 31.7854
            }
        },
        "Parent": {
            "FirstName": "John",
            "LastName": "Green"
        },
        "Processed": "false",
        "Type": "PERSON"
    },
}

CodePudding user response:

You can't have two Person keys in the desired output. But, You can use one of these jolt specs according to your desired output:

1. Save all person objects in an array

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "people": {
          "*": {
            "uniqueIdentifier": "Person[&1].Id",
            "type": "Person[&1].Type",
            "parent": {
              "firstName": "Person[&2].Parent.FirstName",
              "lastName": "Person[&2].Parent.LastName"
            },
            "common": {
              "firstName": "Person[&2].FirstName",
              "lastName": "Person[&2].LastName",
              "timeOfReporting": "Person[&2].DateCreated",
              "geoLocation": {
                "geometry": {
                  "point": {
                    "coordinates": {
                      "0": "Person[&6].Locations.Location.Longitude",
                      "1": "Person[&6].Locations.Location.Latitude"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "*": {
          "Processed": "false"
        }
      }
    }
  }
]

2. Save each person's object in a new object:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "people": {
          "*": {
            "uniqueIdentifier": "Person.&1.Id",
            "type": "Person.&1.Type",
            "parent": {
              "firstName": "Person.&2.Parent.FirstName",
              "lastName": "Person.&2.Parent.LastName"
            },
            "common": {
              "firstName": "Person.&2.FirstName",
              "lastName": "Person.&2.LastName",
              "timeOfReporting": "Person.&2.DateCreated",
              "geoLocation": {
                "geometry": {
                  "point": {
                    "coordinates": {
                      "0": "Person.&6.Locations.Location.Longitude",
                      "1": "Person.&6.Locations.Location.Latitude"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "*": {
          "Processed": "false"
        }
      }
    }
  }
]
  • Related