let data = [
{
system: { id: "4gSSbjCFEorYXqrgDIP2FA", type: "Entry", name: "Author" },
DataDetails: {
shortSlugOption: { "en-us": "some value", "za-op": "random value" },
mediaFileAssetLink: { "en-us": "some file", "za-op": "file Linl" },
mediaFileAssetGalary: { hi: "file link 2" },
singleMediaImage: { hi: "file link single", "en-us": "english link" },
},
},
{
system: { id: "1aBOO8tu3lUsjtICuIbUM5", type: "Entry", name: "Author" },
DataDetails: {
short: { "en-us": "details of shorts", "za-op": "short details" },
shortSlugOption: { "hi-In": "options" },
booleanField: { "hi-In": "true" },
},
},
{
system: { id: "2pOUGnI1oRD7nsrYs600HA", type: "Entry", name: "testing" },
DataDetails: { testingNewValue: { "en-us": "details value" } },
},
{
system: { id: "66rzYr2BpWL1VTBHdLTdSW", type: "Entry", name: "new" },
DataDetails: {
oneReference: { hi: "values 1" },
multiReference: { "hi-In": "values 2" },
},
},
{
system: { id: "cIb5mqEBRWDD6hrNmFmFE", type: "Entry", name: "new" },
DataDetails: { testingNewValue: { "hi-IN": "jksdsdo" } },
},
{
system: { id: "7kRzyt4PFrX13gHcw3Z1Ko", type: "Entry", name: "testing" },
DataDetails: { testingNewValue: { "en-us": "kknksdo" } },
},
{
system: { id: "2OspeCtNK0sh2cPiuU9jIz", type: "Entry", name: "dummy" },
DataDetails: {
short: { "za-op": "dfvndkssa" },
shortSlugOption: { hi: "sdocjosmdc" },
mediaFileAssetLink: { "en-us": "jdsojocis" },
booleanField: { "hi-In": "true" },
},
},
{
system: { id: "2eAmIIuG4xkLvatkU3RUSy", type: "Entry", name: "dummy" },
DataDetails: {
dummy: { "en-us": "dshcifdvk" },
india: { "za-op": "sdci", hi: "hewd" },
},
},
{
system: { id: "7hbdS3MgfZ73TOtlu1WfXw", type: "Entry", name: "dummy" },
DataDetails: {
testingNewValue: { "en-us": "sdcoklsdc" },
locationField: { hi: "sdcndkdc" },
},
},
];
let res = data.reduce((acc, curr) => {
if (!acc[curr.system.name]) {
acc[curr.system.name] = {};
}
let detailsObj = {};
Object.keys(curr.DataDetails).forEach((detail) => {
detailsObj[detail] = Object.values(curr.DataDetails[detail])[0];
});
acc[curr.system.name][curr.system.id] = {
...detailsObj,
};
return acc;
}, {});
console.log(res);
I am storing my data in JSON file but by doing some modification in my current object.
data I want to store in JSON is like in this way
Like Data present
- Author
- en-us/zo-ap / other key which are available in obj
- 4gSSbjCFEorYXqrgDIP2FA/ 1aBOO8tu3lUsjtICuIbUM5
- then the remaining json object
Expected Output
{
"Author": {
"en-us": {
"4gSSbjCFEorYXqrgDIP2FA": {
"shortSlugOption": "some value",
"mediaFileAssetLink": "some file",
"singleMediaImage": "english link"
},
"1aBOO8tu3lUsjtICuIbUM5": {
"short":"details of shorts"
}
},
"za-op": {
"4gSSbjCFEorYXqrgDIP2FA": {
"shortSlugOption": "random value",
"mediaFileAssetLink":"file Linl",
},
"1aBOO8tu3lUsjtICuIbUM5": {
"short":"short details",
}
},
"hi": {
"4gSSbjCFEorYXqrgDIP2FA": {
"mediaFileAssetGalary": "file link 2",
"singleMediaImage":"file link single"
}
},
"hi-In": {
"1aBOO8tu3lUsjtICuIbUM5": {
"shortSlugOption": "options",
"booleanField":true
}
}
},
"dummy": {
"en-us": {
"2OspeCtNK0sh2cPiuU9jIz": {
"mediaFileAssetLink":"jdsojocis",
},
"2eAmIIuG4xkLvatkU3RUSy": {
"dummy": "dshcifdvk"
}
"7hbdS3MgfZ73TOtlu1WfXw": {
"testingNewValue":"sdcoklsdc"
}
},
"za-op": {
"2OspeCtNK0sh2cPiuU9jIz": {
"short":"dfvndkssa",
},
"2eAmIIuG4xkLvatkU3RUSy": {
"india":"sdci"
}
},
"hi": {
"2OspeCtNK0sh2cPiuU9jIz": {
"shortSlugOption":"sdocjosmdc"
},
"2eAmIIuG4xkLvatkU3RUSy": {
"india":"hewd"
}
}
"hi-In": {
"2OspeCtNK0sh2cPiuU9jIz": {
"booleanField":"true"
},
"7hbdS3MgfZ73TOtlu1WfXw": {
"locationField":"sdcndkdc"
}
}
},
"new": {
"hi": {
"66rzYr2BpWL1VTBHdLTdSW": {
"oneReference":"values 1"
}
},
"hi-In": {
"66rzYr2BpWL1VTBHdLTdSW": {
"multiReference": "values 2" ,
},
"hi-IN": {
"cIb5mqEBRWDD6hrNmFmFE": {
"testingNewValue":"jksdsdo"
}
}
}
}.
"testing": {
"en-us": {
"2pOUGnI1oRD7nsrYs600HA": {
"testingNewValue":"details value"
},
"7kRzyt4PFrX13gHcw3Z1Ko": {
"testingNewValue":"kknksdo"
}
},
}
}
As in above code the en-us hi hi-In hi-IN zo-ap or any other key are getting removed
but I want to create a nested json object by this name or any other name which are present inside json
CodePudding user response:
You need the nested entries and use the keys in changed order.
const
data = [{ system: { id: "4gSSbjCFEorYXqrgDIP2FA", type: "Entry", name: "Author" }, DataDetails: { shortSlugOption: { "en-us": "some value", "za-op": "random value" }, mediaFileAssetLink: { "en-us": "some file", "za-op": "file Linl" }, mediaFileAssetGalary: { hi: "file link 2" }, singleMediaImage: { hi: "file link single", "en-us": "english link" } } }, { system: { id: "1aBOO8tu3lUsjtICuIbUM5", type: "Entry", name: "Author" }, DataDetails: { short: { "en-us": "details of shorts", "za-op": "short details" }, shortSlugOption: { "hi-In": "options" }, booleanField: { "hi-In": "true" } } }, { system: { id: "2pOUGnI1oRD7nsrYs600HA", type: "Entry", name: "testing" }, DataDetails: { testingNewValue: { "en-us": "details value" } } }, { system: { id: "66rzYr2BpWL1VTBHdLTdSW", type: "Entry", name: "new" }, DataDetails: { oneReference: { hi: "values 1" }, multiReference: { "hi-In": "values 2" } } }, { system: { id: "cIb5mqEBRWDD6hrNmFmFE", type: "Entry", name: "new" }, DataDetails: { testingNewValue: { "hi-IN": "jksdsdo" } } }, { system: { id: "7kRzyt4PFrX13gHcw3Z1Ko", type: "Entry", name: "testing" }, DataDetails: { testingNewValue: { "en-us": "kknksdo" } } }, { system: { id: "2OspeCtNK0sh2cPiuU9jIz", type: "Entry", name: "dummy" }, DataDetails: { short: { "za-op": "dfvndkssa" }, shortSlugOption: { hi: "sdocjosmdc" }, mediaFileAssetLink: { "en-us": "jdsojocis" }, booleanField: { "hi-In": "true" } } }, { system: { id: "2eAmIIuG4xkLvatkU3RUSy", type: "Entry", name: "dummy" }, DataDetails: { dummy: { "en-us": "dshcifdvk" }, india: { "za-op": "sdci", hi: "hewd" } } }, { system: { id: "7hbdS3MgfZ73TOtlu1WfXw", type: "Entry", name: "dummy" }, DataDetails: { testingNewValue: { "en-us": "sdcoklsdc" }, locationField: { hi: "sdcndkdc" } } }],
result = data.reduce((r, { system: { id, type, name }, DataDetails }) => {
r[name] ??= {};
Object.entries(DataDetails).forEach(([k, o]) => {
Object.entries(o).forEach(([l, v]) => {
r[name][l] ??= {};
r[name][l][id] ??= {};
r[name][l][id][k] = v;
});
});
return r;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }