I am wondering if it is possible to normalize such JSON:
{
"rows": [{
"cells": [{
"value": "Column Name 1"
},
{
"value": "Column Name 2"
},
{
"value": "Column Name 3"
},
{
"value": "Column Name 4"
}
]
},
{
"cells": [{
"value": "Second Row Thing1"
},
{
"value": "Second Row Thing2"
},
{
"value": "Second Row Thing3"
},
{
"value": "Second Row Thing4"
}
]
},
{
"cells": [{
"value": "Third Row Thing1"
},
{
"value": "Third Row Thing2"
},
{
"value": "Third Row Thing3"
},
{
"value": "Third Row Thing4"
}
]
}
]
}
into such nice format:
{
"rows": [{
"Column Name 1": "Second Row Thing1"
"Column Name 2": "Second Row Thing1"
"Column Name 3": "Second Row Thing1"
"Column Name 4": "Second Row Thing1"
},
{
"Column Name 1": "Third Row Thing1"
"Column Name 2": "Third Row Thing1"
"Column Name 3": "Third Row Thing1"
"Column Name 4": "Third Row Thing1"
}
]
}
basically I would like to take first row's data ant treat it like columns names. And then use those column names as names for keys in my row objects. Is it possible to do such thing with "normalizr"? Or should I deep dive into "map", "foreach" and etc array things? :)
You really don't need Normalizr for this (and you shouldn't use it, because it won't work). Normalizr is for nested data: entities that have references to other entities (like users embedded in tweets).
Map/Reduce works really well for something like this. Here's a snippet that does exactly what you're asking.
const data = { "rows": [
{ "cells": [{ "value": "Column Name 1" }, { "value": "Column Name 2" }, { "value": "Column Name 3" }, { "value": "Column Name 4" } ]},
{ "cells": [{ "value": "Second Row Thing1" }, { "value": "Second Row Thing2" }, { "value": "Second Row Thing3" }, { "value": "Second Row Thing4" } ] },
{ "cells": [{ "value": "Third Row Thing1" }, { "value": "Third Row Thing2" }, { "value": "Third Row Thing3" }, { "value": "Third Row Thing4" } ] }
]};
// Get an array of the values of the first row
const columns = data.rows[0].cells.map(({ value }) => value);
// slice = take all rows except the first
// mapp each array of cells
const normalizedData = data.rows.slice(1).map(({ cells }) => {
// reduce = converts the array of cells into an object map
return cells.reduce((memo, { value }, i) => {
memo[columns[i]] = value;
return memo;
}, {});
});
console.log(JSON.stringify(normalizedData, null, 2));