Home > Blockchain >  Jolt how to include object outside list and add it to each object of list
Jolt how to include object outside list and add it to each object of list

Time:10-29

I have an array which I have to iterate and append object that is outside the list to each of the object inside list. It is not happening properly. Since offerings is an array if it has multiple objects to each object I need to have typeId of that object.

Request:

[
  {
    "offers": [
      {
        "type": {
          "id": "5"
        },
        "offerings": [
          {
            "id": "10"
          },
          {
            "id": "9"
          }
        ]
      },
      {
        "type": {
          "id": "3"
        },
        "offerings": [
          {
            "id": "11"
          }
        ]
      },
      {
        "type": {
          "id": "2"
        },
        "offerings": [
          {
            "id": "14"
          },
          {
            "id": "16"
          }
        ]
      }
    ]
  }
]

My spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "offers": {
          "*": {
            "type": {
              "id": "[&4].[&2].typeId"
            },
            "offerings": {
              "*": {
                "id": "[&3].[&1].offerId"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  }
]

Response now:

[
  {
    "typeId": "5",
    "offerId": "10"
  },
  {
    "offerId": "9",
    "typeId": "3"
  },
  {
    "typeId": "2"
  },
  {
    "offerId": "11"
  },
  {
    "offerId": "14"
  },
  {
    "offerId": "16"
  }
]

Expected:

[
  {
    "typeId": "5",
    "offerId": "10"
  },
  {
    "typeId": "5",
    "offerId": "9"
  },
  {
    "typeId": "3",
    "offerId": "11"
  },
  {
    "typeId": "2",
    "offerId": "14"
  },
  {
    "typeId": "2",
    "offerId": "16"
  }
]

Please help to achieve as expected.

CodePudding user response:

You can transfer "id": "[&4].[&2].typeId" key-value pair into the object tagged "offerings" key name as "@(2,type.id)": "[&3].[&1].typeid" in order to combine them as desired such like the below one

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "offerings": {
              "*": {
                "@(2,type.id)": "[&3].[&1].typeid",
                "*": "[&3].[&1].offer&"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
   }
 ]

enter image description here

  • Related