Home > Back-end >  Converting one json object to another with jquery
Converting one json object to another with jquery

Time:06-15

i have an input json object , and i want the below output json from a jquery function . How can i achieve it with min lines of code .

Input

{
    "dashboardWidgetFacility": [{
        "deskUsageInput": {
            "breakup": [{
                "location_breakup_0": ["{\"id\":1,\"name\":\"campus\"},{\"id\":8,\"name\":\"building \"}"],
                "teams_breakup_0": ["3", "6"]
            }, {
                "location_breakup_1": ["{\"id\":1,\"name\":\"campus\"},{\"id\":8,\"name\":\"building \"},{\"id\":83,\"name\":\"floor \"}"],
                "teams_breakup_1": ["3", "2"]
            }]
        }
    }]
} 

Output Object

{
    "dashboardWidgetFacility": [{
        "campus": {
            "id": 1
        },
        "building": {
            "id": 8
        },
        "widgetInput": {
            "deskUsageInput": {
                "teams": ["3", "6"]
            }
        }
    }, {
        "campus": {
            "id": 1
        },
        "building": {
            "id": 8
        },
        "floor": {
            "id": 83
        },
        "widgetInput": {
            "deskUsageInput": {
                "teams": ["3", "2"]
            }
        }
    }]
}

Here location breakup location_breakup_{index} , has the index.

CodePudding user response:

I had to correct those location_breakup values because you have to parse out the json, but theyre not technically valid json on their own since theyre missing the square brackets on the sides.

const input = {
  dashboardWidgetFacility: [
    {
      deskUsageInput: {
        breakup: [
          {
            location_breakup_0: [
              '{"id":1,"name":"campus"},{"id":8,"name":"building "}',
            ],
            teams_breakup_0: ['3', '6'],
          },
          {
            location_breakup_1: [
              '{"id":1,"name":"campus"},{"id":8,"name":"building "},{"id":83,"name":"floor "}',
            ],
            teams_breakup_1: ['3', '2'],
          },
        ],
      },
    },
  ],
};

const output = Object.values(
  input.dashboardWidgetFacility[0].deskUsageInput.breakup
).reduce(
  (acc, item) => {
    const nestedItem = Object.keys(item).reduce((acc, key) => {
      let locationParsed;
      let teamsBreakup;
      if (key.startsWith('location_breakup')) {
        locationParsed = JSON.parse('['   item[key]   ']');
      }
      if (key.startsWith('teams_breakup')) {
        teamsBreakup = item[key];
      }

      const obj = (locationParsed || []).reduce(
        (acc, item) => {
          const { name, id } = item; 
          return {
            ...acc,
            [name.trim()]: {
              id,
            },
          };
        },
        {
          widgetInput: {
            deskUsageInput: {
              teams: null,
            },
          },
        }
      );
      obj.widgetInput.deskUsageInput.teams = teamsBreakup;
      return {
        ...acc,
        ...obj,
      };
    }, {});
    return {
      dashboardWidgetFacility: [...acc.dashboardWidgetFacility, nestedItem],
    };
  },
  {
    dashboardWidgetFacility: [],
  }
);

console.log(output);

  • Related