Generic method to convert a flat JSON array to nested JSON
By : unaior
Date : March 29 2020, 07:55 AM
Does that help If you use Json.Net, you can do this conversion in a generic way using the LINQ-to-JSON API (JObjects). The idea is to parse the JSON array and add all the individual items to a dictionary keyed by Id. Then, loop over the dictionary items, and for each one, try to look up the parent. If the parent is found, add the item to the parent's items array (creating it if needed). Otherwise, add the item to the root array. Along the way, remove the depth property from each item, since you don't seem to want that in the output. Lastly, just dump the root array to string to get the final result. code :
var dict = JArray.Parse(json)
.Children<JObject>()
.ToDictionary(jo => (string)jo["Id"], jo => new JObject(jo));
var root = new JArray();
foreach (JObject obj in dict.Values)
{
JObject parent;
string parentId = (string)obj["ParentId"];
if (parentId != null && dict.TryGetValue(parentId, out parent))
{
JArray items = (JArray)parent["items"];
if (items == null)
{
items = new JArray();
parent.Add("items", items);
}
items.Add(obj);
}
else
{
root.Add(obj);
}
JProperty depth = obj.Property("depth");
if (depth != null) depth.Remove();
}
Console.WriteLine(root.ToString());
|
jq: How to convert flat json into nested one
By : Thatsakhorn Suwanjir
Date : March 29 2020, 07:55 AM
I wish this help you I've been trying to re-arrange a pretty flat json into structure with more depth, so far without any success. Here's my source data: , jq solution: code :
jq '[group_by(.service_name)[]
| .[0].service_name as $name
| { name: $name,
data: map({ date: .time,
details: {event, version}})
}]' jsonfile
[
{
"name": "cart",
"data": [
{
"date": "2017-12-21 07:21:00",
"details": {
"event": "Success",
"version": "9ff24c4"
}
}
]
},
{
"name": "prices",
"data": [
{
"date": "2017-12-21 07:24:00",
"details": {
"event": "Success",
"version": "61f4u8e"
}
},
{
"date": "2017-12-21 07:23:00",
"details": {
"event": "Fail",
"version": "21c2f7d"
}
}
]
}
]
|
Convert nested Json to flat Json with parentId to every node
By : user2007710
Date : March 29 2020, 07:55 AM
around this issue You could take an iterative and recursive approach by using a function which takes an array and a parent id for the actual level. If a property starts with child, it calls the function again with the actual _id and pushes all items to the result set. code :
function getFlat(array, parentid) {
return array.reduce((r, o) => {
var temp = {};
r.push(temp);
Object.entries(o).forEach(([k, v]) => {
if (k.startsWith('child')) {
r.push(...getFlat(v, o._id));
} else {
temp[k] = v;
}
});
temp.parentid = parentid;
return r;
}, []);
}
var data = [{ child1: [{ _type: "EntityChild1", name: "Test222", _id: 2 }], child2: [{ _type: "EntityChild2", name: "Test333", _id: 3, child2_child1: [{ _type: "EntityChild2_1", name: "Test444", _id: 6, child2_child1_child1: [{ _type: "EntityChild2_1_1", name: "Test555", _id: 7 }] }] }], _type: "EntityParent", name: "Test000", _id: 1, child3: [{ _type: "EntityChild3", name: "Test111", _id: 4 }], child4: [{ _type: "EntityChild4", name: "Test666", _id: 5 }] }],
flat = getFlat(data, -1);
console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }
|
How can I convert a nested part of a JSON object to dot-chained flat JSON?
By : creezi smith
Date : March 29 2020, 07:55 AM
I hope this helps you . You evidently already know the solution to the second part of the problem, so here is a solution to the first part: code :
# linearize all paths of the input object or array
def dotkeys:
def stringify: map(tostring) | join(".");
. as $in
| reduce paths(scalars) as $p (null;
($in|getpath($p)) as $v
| setpath([$p|stringify]; $v));
def chain(key):
.[key] |= dotkeys ;
chain("level1b")
{
"level1a": "value",
"level1b": {
"level2a.level3a": "nestedvalue",
"level2b": "value"
}
}
|
unable to convert nested json to flat json using javascript
By : Samster357
Date : October 08 2020, 07:00 AM
This might help you You can simply us Array.flatMap() or Array.map() methods to return a new array. As per your requirement, both will work. Array.flatMap() & Array.map() code :
var data = [{"_score":0.5753642,"_type":"data","_id":"686","_source":{"mainData":"subset","data":"vehicle","name":"subset_vehicle","fields":["number","id","chasis"],"dataset":"chasis"},"_index":"vehicle"},{"_score":0.575,"_type":"data","_id":"687","_source":{"mainData":"subset1","data":"vehicle1","name":"subset_vehicle1","fields":["number","id","chasis"],"dataset":"chasis1"},"_index":"vehicle"},{"_score":0.57,"_type":"data","_id":"686","_source":{"mainData":"subset","data":"vehicle","name":"subset_vehicle","fields":["number","id","chasis"],"dataset":"chasis"},"_index":"vehicle"}]
var a = data.flatMap(d => {
d._source._id = d._id;
return d._source;
})
console.log(a)
|