admin管理员组文章数量:1353662
I am using javascript and I have nested json object getting from mongodb.
"abc": [
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"c": [
{
"ca": "A",
"cb": "AflJufPlFStqKBZ",
"cc": "S008400"
},
{
"cx": "A",
"cy": "AflJufPlFStqKBZ",
"cz": "S008400"
}
]
},
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"c": [
{
"ca": "A",
"cb": "AflJufPlFStqKBZ",
"cc": "S008400"
},
{
"cx": "A",
"cy": "AflJufPlFStqKBZ",
"cz": "S008400"
}
]
}
]
Above schema have fixed fields there will no changes in schema.
Now I want to make it as flat json array object and result should be like that. If c
has multiple json object the it should create a new json object with the same a
, b
value
[{
"a": "01AABCE2207R1Z5",
"b": "Y",
"ca": "A",
"cb": "AflJufPlFStqKBZ",
"cc": "S008400"
},
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"cx": "A",
"cy": "AflJufPlFStqKBZ",
"cz": "S008400"
},
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"ca": "A",
"cb": "AflJufPlFStqKBZ",
"cc": "S008400"
},
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"cx": "A",
"cy": "AflJufPlFStqKBZ",
"cz": "S008400"
}
]
So, I want to know the fast and easy steps to make it flat. Please let me know the process and methods to solve this.
Thanks
I am using javascript and I have nested json object getting from mongodb.
"abc": [
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"c": [
{
"ca": "A",
"cb": "AflJufPlFStqKBZ",
"cc": "S008400"
},
{
"cx": "A",
"cy": "AflJufPlFStqKBZ",
"cz": "S008400"
}
]
},
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"c": [
{
"ca": "A",
"cb": "AflJufPlFStqKBZ",
"cc": "S008400"
},
{
"cx": "A",
"cy": "AflJufPlFStqKBZ",
"cz": "S008400"
}
]
}
]
Above schema have fixed fields there will no changes in schema.
Now I want to make it as flat json array object and result should be like that. If c
has multiple json object the it should create a new json object with the same a
, b
value
[{
"a": "01AABCE2207R1Z5",
"b": "Y",
"ca": "A",
"cb": "AflJufPlFStqKBZ",
"cc": "S008400"
},
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"cx": "A",
"cy": "AflJufPlFStqKBZ",
"cz": "S008400"
},
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"ca": "A",
"cb": "AflJufPlFStqKBZ",
"cc": "S008400"
},
{
"a": "01AABCE2207R1Z5",
"b": "Y",
"cx": "A",
"cy": "AflJufPlFStqKBZ",
"cz": "S008400"
}
]
So, I want to know the fast and easy steps to make it flat. Please let me know the process and methods to solve this.
Thanks
Share Improve this question edited Jun 20, 2017 at 9:24 Saurabh Sharma asked Jun 20, 2017 at 9:09 Saurabh SharmaSaurabh Sharma 8047 gold badges16 silver badges42 bronze badges 6- It's so easy to do... What did you try? – laurent Commented Jun 20, 2017 at 9:13
- You have not even tagged this with a language you want to do this in ... And you should have at least an approach you e up with yourself to show us. – C3roe Commented Jun 20, 2017 at 9:13
- Try npmjs./package/flat – Igor Popov Commented Jun 20, 2017 at 9:14
-
I am using javascript @CBroe. and I tried by using
forloop
but when I m doing it with forloop its became a lengthy process. – Saurabh Sharma Commented Jun 20, 2017 at 9:15 -
One loop over the main level items, and inside another loop that gets everything out of the
c
sub structure, places it on the same main level, afterwards remove the originalc
... "how hard can it be?" – C3roe Commented Jun 20, 2017 at 9:18
2 Answers
Reset to default 5It is so easy to do this.
var flatArray = [];
var flatObject = {};
for (var index = 0; index < data.length; index++) {
for (var prop in data[index]) {
var value = data[index][prop];
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
for (var inProp in value[i]) {
flatObject[inProp] = value[i][inProp];
}
}
}else{
flatObject[prop] = value;
}
}
flatArray.push(flatObject);
}
console.log(flatArray);
data is your array.
this will flat JSONObject even that includes JSONArray in it ...please use it ..will work
const flatJSONObject = r => {//flaten the Json in full depth
const oc = ({}).constructor;
const ac = ([]).constructor;
var o={};
if(r.constructor === oc)
{
for (var k in r) {
if ( r[k].constructor === oc) o = {...o,...flatJSONObject(value)}
else if ( r[k].constructor === ac) r[k].forEach(e =>o = {...o,...flatJSONObject(e)});
else o[k] = r[k];
}
}
return o;
}
本文标签: javascriptConvert Nested JSON to Flat JSONStack Overflow
版权声明:本文标题:javascript - Convert Nested JSON to Flat JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743934125a2564377.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论