Search code examples
karatekarate-call-single

How to generate the json array dynamically based on the Input Array - language wise


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.


Solution

  • 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));