简体   繁体   中英

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

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.

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

在此处输入图片说明

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