Home > database >  JSON data, need modifications
JSON data, need modifications

Time:10-13

I have a problem with converting Json data , I get a list objects with different categories for each category there are two versions , So I need to modify the in a way to get list of objects where each object key is the category and its value is list of its corresponding versions and details.

data = [
         {
            "category1": {
                "name": "category1",
                "id": 1,
                "last_sync_date": "None",
                "version": "Version 1"
            }
        },
        {
            "category1": {
                "name": "category1",
                "id": 2,
                "last_sync_date": "None",
                "version": "Version 2"
            }
        },
         {
          "category2": {
                "name": "category2",
                "id": 1,
                "last_sync_date": "None",
                "version": "Version 1"
            }
        },
        {
            "category3": {
                "name": "category3",
                "id": 2,
                "last_sync_date": "None",
                "version": "Version 2"
            }
        },
]

To:

data = {
category1: [
    {
      id: "1",
      name: "category1",
      last_sync_date: "01/01/2022 10:45 AM",
      version: "Version 1"
    },
    {
      id: "2",
      name: "category1",
      last_sync_date: "01/01/2022 10:45 AM",
      version: "Version 2"
    },

  ],
category2:[
{
      id: "3",
      name: "category2",
      last_sync_date: "01/01/2022 10:45 AM",
      version: "Version 1"
    },

],
category3:[
{
      id: "4",
      name: "category3",
      last_sync_date: "01/01/2022 10:45 AM",
      version: "Version 1"
    },

],
}
  

Note: I was able to provide only one unit of data.

CodePudding user response:

This is not JSON btw, this is just a JavaScript Array of objects that needs to be converted into another structure.

This can be done with any of the loop logics. I can show you one example with Array.reduce

Logic

  • Loop through data array using Array.reduce
  • Take each object and get the keys of each object.
  • Check whether this key exist in out accumulator object
  • If the key exist, push the Object to existing key, else create an empty array as the value for that key and push our object to the emty array just created.

const data = [
  {
    category1: { name: "category1", id: 1, last_sync_date: "None", version: "Version 1" },
  },
  {
    category1: { name: "category1", id: 2, last_sync_date: "None", version: "Version 2" },
  },
  {
    category2: { name: "category2", id: 1, last_sync_date: "None", version: "Version 1" },
  },
  {
    category3: { name: "category3", id: 2, last_sync_date: "None", version: "Version 2" },
  },
];
const output = data.reduce((acc, curr) => {
  const keys = Object.keys(curr);
  keys.forEach((key) => {
    acc[key] = acc[key] || [];
    acc[key].push(curr[key]);
  });
  return acc;
}, {});
console.log(output);

CodePudding user response:

The following uses Array#reduce to enumerate the data. It pulls out the category and the object associated with each item. The objects are then added to a result object, keyed by category and grouped within arrays.

Does this do what you want?

const data = [{
    "category1": {
        "name": "category1",
        "id": 1,
        "last_sync_date": "None",
        "version": "Version 1"
    }
}, {
    "category1": {
        "name": "category1",
        "id": 2,
        "last_sync_date": "None",
        "version": "Version 2"
    }
}, {
    "category2": {
        "name": "category2",
        "id": 1,
        "last_sync_date": "None",
        "version": "Version 1"
    }
}, {
    "category3": {
        "name": "category3",
        "id": 2,
        "last_sync_date": "None",
        "version": "Version 1"
    }
}, ]

const group = (data) =>
    data.reduce((acc, c) => {
        const [[category, value]] = Object.entries(c)
        acc[category] 
            ? acc[category].push(value) 
            : acc[category] = [value]
        return acc
    }, {})

console.log(group(data))

CodePudding user response:

This is not a JSON structure. Input is an array of objects. According to your description you just want to convert it to another structure.

Here is a sample solution of this problem.

// const data = [{}, {}...]; Input array of objects

const ans = {};
data.map(object => {
    const keys = Object.keys(object);// to get keys of current object
    const firstKey = keys[0];
    const value = object[firstKey];

    if (ans[firstKey] == undefined) {
        ans[firstKey] = [value];
    } else {
        ans[firstKey].push(value);
    }
})

console.log(ans);

  • Related