1

Question on JSON Transformation

I have a request JSON object as follows:

def request = {
  "Items": [
    {
      "id": "151",
      "optionNumber": 1,
      "ListItems": [
        {
          "language": "en",
          "value": "Aruba"
        },
        {
          "language": "en-gb",
          "value": "Arubagb"
        },
        {
          "language": "fr-fr",
          "value": ""
        }
      ]
    },
    {
      "id": "152",
      "optionNumber": 2,
      "ListItems": [
        {
          "language": "en",
          "value": "Afganistan"
        },
        {
          "language": "en-us",
          "value": ""
        }
      ]
    }
  ]
}

Each item in the ListItems array contains a language and a value. I need to generate an output array based on the language. The desired output structure is as follows:

{
  "Response": [
    {
      "language": "en",
      "optionValues": [
        {
          "optionNumber": 1,
          "optionValue": "Aruba"
        },
        {
          "optionNumber": 2,
          "optionValue": "Afganistan"
        }
      ]
    },
    {
      "language": "en-gb",
      "optionValues": [
        {
          "optionNumber": 1,
          "optionValue": "Arubagb"
        }
      ]
    },
    {
      "language": "fr-fr",
      "optionValues": [
        {
          "optionNumber": 1,
          "optionValue": ""
        }
      ]
    }
  ]
}

Please guide me on how to transform the input JSON into the desired output structure.

Shreyansh Jain
  • 498
  • 4
  • 7
  • please read up on the `map()` function, that's all you need, refer: https://stackoverflow.com/a/76091034/143475 and the karate documentation, search for "json transforms". I pass on this question otherwise – Peter Thomas Jun 30 '23 at 17:40
  • @Peter Thomas, I tried converting the below JS Code to equivalent Karate code using appendTo (as per the documentation, this is equivalent to push() of JS ), however it did not work. Can you please suggest the equivalent Karate code for the below code. group[listItem.language] = group[listItem.language] || []; group[listItem.language].push({ optionNumber: item.optionNumber, optionValue: listItem.value, }); – Shreyansh Jain Jul 02 '23 at 06:50
  • read the link in my first comment more carefully, using `karate.append(array, val)` is not needed any more, `array.push(val)` is good enough. if you still have a question, ask a new one and **keep it simple** – Peter Thomas Jul 02 '23 at 07:36

1 Answers1

1

We can use the map and then form the desire response.

function getResp(data) {
  const group = {};

  // Filter Active Data
  const activeData = data?.Items.filter((item) => item.status === "ACTIVE");

  activeData.forEach((item) => {
    item?.ListItems.forEach((listItem) => {
      group[listItem.language] = group[listItem.language] || [];
      group[listItem.language].push({
        optionNumber: item.optionNumber,
        optionValue: listItem.value,
      });
    });
  });

  const respData = Object.entries(group).map(([key, value]) => ({
    language: key,
    optionValues: value,
  }));
  return { Response: respData };
}

const data = {
  Items: [
    {
      id: "151",
      optionNumber: 1,
      status: "ACTIVE",
      ListItems: [
        {
          language: "en",
          value: "Aruba",
        },
        {
          language: "en-gb",
          value: "Arubagb",
        },
        {
          language: "fr-fr",
          value: "",
        },
      ],
    },
    {
      id: "152",
      optionNumber: 2,
      status: "INACTIVE",
      ListItems: [
        {
          language: "en",
          value: "Afganistan",
        },
        {
          language: "en-us",
          value: "",
        },
      ],
    },
  ],
};

console.log(getResp(data));
Anuj Gupta
  • 26
  • 2