Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Converting one json object to another with jquery

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.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

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

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!