Normalize JSON to a custom schema - javascript

I have an array of objects with the following format
var arr = [
{
"productId": "123456",
"productName": "Test Product 1",
"description": [
"This is delicious",
"Suitable for vegetarian"
],
"attributes": {
"internalId": "091283"
"category": "Dairy"
},
"order": 1
}
];
And I am trying to map into something like below
[
[{
{
"name": "productId",
"value": "123456"
},
{
"name": "productName",
"value": "Test Product 1"
},
{
"name": "description",
"value": ["This is delicious", "Suitable for vegetarian"]
},
{
"name": "attributes",
"value": {
{
"name": "internalId",
"value": "091283"
},
{
"name": "category",
"value": "Dairy"
}
}
},
{
"name": "order",
"value": 1
}
}]
]
I tried mapping simple properties before going further and now stuck at getting only the last property of each object in the loop.
Suppose I don't know what are the format of incoming data and how can I normalize the JSON object to the format I want?
normalizeJson = (array) => {
for(i = 0; i < array.length; i++){
normalizedJson[i] = {};
Object.keys(array[i]).forEach(key => {
if (array[i][key] && typeof array[i][key] === "object") {
// normalizeJson(obj[key]);
// console.log(key + ' is object');
return;
} else {
o = {};
o["name"] = key;
o["value"] = array[i][key];
normalizedJson[i] = o;
// normalizedJson[i]["name"] = key;
// normalizedJson[i].value = array[i][key];
// console.log(key);
return;
}
});
}
console.log(normalizedJson);
};
Or is there any library I can use in order to achieve this?

Try this
var obj = [
{
productId: "123456",
productName: "Test Product 1",
description: ["This is delicious", "Suitable for vegetarian"],
attributes: {
internalId: "091283",
category: "Dairy",
},
order: 1,
},
];
function normalizeObject(obj) {
var result = [];
if (Array.isArray(obj)) {
for (let i of obj) {
result.push(normalizeObject(i));
}
} else if (typeof obj == "object") {
for (let i of Object.keys(obj)) {
result.push({ name: i, value: normalizeObject(obj[i]) });
}
} else {
return obj;
}
return result;
}
console.log(JSON.stringify(normalizeObject(obj), null, 2));
This looping method called recursion. Which is loop by calling function itself.

Related

How to combine multiple JSON object that have same key and value

How to combine JSON objects in the same response that has the same key and value with javascript? This is my data for example:
{
"data": [
{
"name": "A",
"description": {
"location": "location1",
"floor": "floor1",
},
},
{
"name": "A",
"description": {
"location": "location2",
"floor": "floor1",
},
},
{
"name": "B",
"description": {
"location": "location3",
"floor": "floor3",
},
},
]
}
And turn it into this:
{
"data": [
{
"name": "A",
"description": {
"location": ["location1","location2"],
"floor": "floor1",
},
},
{
"name": "B",
"description": {
"location": "location3",
"floor": "floor3",
},
},
]
}
Basically I am someone who is new to learning javascript. Any help would be very helpful, thank you.
You can do:
const data = {data: [{name: 'A',description: {location: 'location1',floor: 'floor1',},},{name: 'A',description: {location: 'location2',floor: 'floor1',},},{name: 'B',description: {location: 'location3',floor: 'floor3',},},],}
const result = {
data: data.data.reduce((a, { name, description }) => {
const index = a.findIndex((d) => d.name === name)
if (index >= 0) {
let location = a[index].description.location
location = Array.isArray(location) ? location : [location]
a[index].description.location = [...location, description.location]
} else {
a.push({ name, description })
}
return a
}, []),
}
console.log(result)
const list = {
"data": [
{
"name": "A",
"description": {
"location": "location1",
"floor": "floor1",
},
},
{
"name": "A",
"description": {
"location": "location2",
"floor": "floor1",
},
},
{
"name": "B",
"description": {
"location": "location3",
"floor": "floor3",
},
},
]
};
const consolidatedData = [];
for (const ele of list.data) {
const isExist = consolidatedData.find(x => x.name === ele.name);
if (!isExist) {
consolidatedData.push({
...ele
})
} else {
const objectKey = consolidatedData.findIndex(x => x.name === ele.name);
if (objectKey > -1) {
const description = consolidatedData[objectKey].description;
const newDes = ele.description;
if (newDes.location !== description.location) {
const data = consolidatedData[objectKey].description;
const added = [data.location, ele.description.location];
delete consolidatedData[objectKey].description.location
consolidatedData[objectKey].description["location"] = added
}
if (newDes.floor !== description.floor){
const data = consolidatedData[objectKey].floor;
const added = [data.floor, ele.description.floor];
delete consolidatedData[objectKey].description.floor
consolidatedData[objectKey].description["floor"] = added
}
}
}
}
console.log(JSON.stringify(consolidatedData, null, 2));
Here is a solution that uses an intermediate bucket object. The desired result object is then constructed from the bucket object:
const input = { "data": [ { "name": "A", "description": { "location": "location1", "floor": "floor1", }, }, { "name": "A", "description": { "location": "location2", "floor": "floor1", }, }, { "name": "B", "description": { "location": "location3", "floor": "floor3", }, }, ] };
let buckets = input.data.reduce((acc, obj) => {
if(!acc[obj.name]) {
acc[obj.name] = {
locations: {},
floors: {}
};
}
acc[obj.name].locations[obj.description.location] = true;
acc[obj.name].floors[obj.description.floor] = true;
return acc;
}, {});
console.log('buckets: ', buckets);
let result = {
data: Object.keys(buckets).map(name => {
let locations = Object.keys(buckets[name].locations);
let floors = Object.keys(buckets[name].floors);
return {
name: name,
description: {
location: locations.length == 1 ? locations[0] : locations,
floor: floors.length == 1 ? floors[0] : floors
}
}
})
};
console.log('result:', result);
Notes:
buckets object:
is created using an array .reduce()
array .reduce() docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
locations and floors are collected using objects instead of arrays, this is to avoid duplicate names
result object:
is using Object.keys(buckets) to get the array of names
.map() transforms each name into the desired object
your unusual array or string value for location and floor is constructed with a conditional

how to transform a source object into another object?

I am trying to transform an object or in other words I am trying change text of an object.
I have two object one is source and another is mapped object.using mapped object I want o transform the source object.
example
source object
{
"links": {
"Offering": {
"id":"offering-id",
"iconUrl": "",
"links": [
{
"text": "School",
"id":"school-id",
"onclickUrl": "https://www.school.com/school/"
},
{
"text": "happy",
"onclickUrl": "https://www.happy.com/help/",
"id":"happy-id"
}
]
},
"luck": {
"iconUrl": "",
"id":"hospital-id",
"links": [
{
"text": "Student",
"id":"student-id",
"onclickUrl": "https://www.student.com/school/"
}
]
}
}
}
mapped -object
let mappingObj = {
"hospital-id":"hospital-change",
"school-id":"school-change-xx",
"offering-id":"Offering-changes-map",
"happy-id":"happy-cs",
"student-id":"nnnnnn"
}
expectedout output
let expectedOutput = {
"links": {
"Offering-changes-map": {
"id":"offering-id",
"iconUrl": "",
"links": [
{
"text": "school-change-xx",
"id":"school-id",
"onclickUrl": "https://www.school.com/school/"
},
{
"text": "happy-cs",
"onclickUrl": "https://www.happy.com/help/",
"id":"happy-id"
}
]
},
"hospital-change": {
"iconUrl": "",
"id":"hospital-id",
"links": [
{
"text": "nnnnnn",
"id":"student-id",
"onclickUrl": "https://www.student.com/school/"
}
]
}
}
}
I have id's in source obj same id's are present in mapped obj.using this I want to change the text of all properties and keys.
I tried like that here is my whole code
https://jsbin.com/zaxoyineme/edit?js,console,output
let result = {};
function recursiveFn(Obj){
for(let i in Obj){
result[mappingObj[Obj[i].id] || i]=Obj[i];
if(typeof Obj[i]==="object"){
console.log('--obj--');
recursiveFn(Obj[i]);
}else if(Object.prototype.toString.call(Obj[i]) === '[object Array]'){
console.log('--Array--');
for(var k =0 ;k<Obj[i].length;k++){
Obj[i].text = result[mappingObj[Obj[i].id] || Obj[i].text]
}
}
}
}
for(let i in Obj){
result[mappingObj[Obj[i].id] || i]=Obj[i];
if(typeof Obj[i]==="object"){
console.log('----');
recursiveFn(Obj[i])
}else if(Object.prototype.toString.call(Obj[i]) === '[object Array]'){
console.log('--Array--');
for(var k =0 ;k<Obj[i].length;k++){
Obj[i].text = result[mappingObj[Obj[i].id] || Obj[i].text]
}
}
}
console.log(JSON.stringify(result))
I am using recession to get the expected output.but I am not able to get the expected output
Your code is way to complicated for what you are trying to do. From What I understood this should probably do your job.
let Obj = {
"links": {
"Offering": {
"id":"offering-id",
"iconUrl": "",
"links": [
{
"text": "School",
"id":"school-id",
"onclickUrl": "https://www.school.com/school/"
},
{
"text": "happy",
"onclickUrl": "https://www.happy.com/help/",
"id":"happy-id"
}
]
},
"luck": {
"iconUrl": "",
"id":"hospital-id",
"links": [
{
"text": "Student",
"id":"student-id",
"onclickUrl": "https://www.student.com/school/"
}
]
}
}
}
let mappingObj = {
"hospital-id":"hospital-change",
"school-id":"school-change-xx",
"offering-id":"Offering-changes-map",
"happy-id":"happy-cs",
"student-id":"nnnnnn"
}
// expected output
let expectedOutput = {
"links": {
"Offering-changes-map": {
"id":"offering-id",
"iconUrl": "",
"links": [
{
"text": "school-change-xx",
"id":"school-id",
"onclickUrl": "https://www.school.com/school/"
},
{
"text": "happy-cs",
"onclickUrl": "https://www.happy.com/help/",
"id":"happy-id"
}
]
},
"hospital-change": {
"iconUrl": "",
"id":"hospital-id",
"links": [
{
"text": "nnnnnn",
"id":"student-id",
"onclickUrl": "https://www.student.com/school/"
}
]
}
}
}
let result = {};
let midResult = {};
function updateObject (Obj) {
let i = 0;
let parseArray = Object.keys(Obj.links);
for (i =0; i < parseArray.length; i++) {
let id = parseArray[i]
let matchingId = mappingObj[Obj.links[id].id]
midResult[matchingId] = Object.assign({}, Obj.links[id])
}
}
updateObject(Obj);
result.links = Object.assign({}, midResult)
console.log(result)

Append a key value pair by copying the value of a specific key in javascript in nested json structure

I have a nested json structure like below
const data = {
"name": "A",
"invalid": {
"distinctCountOnColumns": [
{
"key": "some_key",
"value": 101557856
}
],
"groupByAndCountOnColumns": [
[
{
"key": "some_key",
"value": "no data",
"count": 101557856
}
],
]
},
"children": [
{
"name": "B",
"count": 1654164,
"children": [
{
"name": "B1",
"count": 16564,
},
{
"name": "B2",
"count": 165411,
}
]
},
{
"name": "C",
"count": 15135
}
]
}
I want to append a key called value next to the key count by copying the value of the key count. But the key count inside the object invalid should not be considered.
The resultant data is given below
const resultData = {
"name": "A",
"invalid": {
"distinctCountOnColumns": [
{
"key": "some_key",
"count": 192869,
"value": 101557856
}
],
"groupByAndCountOnColumns": [
[
{
"key": "some_key",
"value": "no data",
"count": 101557856
}
],
]
},
"children": [
{
"name": "B",
"count": 1654164,
"value": 1654164,
"children": [
{
"name": "B1",
"count": 16564,
"value": 16564
},
{
"name": "B2",
"count": 165411,
"value": 165411
}
]
},
{
"name": "C",
"count": 15135,
"value": 15135
}
]
}
I tried this method but the key value is not getting added next to the key count.
const deepCopy = (arr) => {
let copy = [];
arr.forEach(elem => {
if(Array.isArray(elem)){
copy.push(deepCopy(elem))
}else{
if (typeof elem === 'object') {
copy.push(deepCopyObject(elem))
} else {
copy.push(elem)
}
}
})
return copy;
};
const deepCopyObject = (obj) => {
let tempObj = {};
for (let [key, value] of Object.entries(obj)) {
if(key !== "invalid" && key === "count"){
obj.value = obj[key];
}
if (Array.isArray(value)) {
tempObj[key] = deepCopy(value);
} else {
if (typeof value === 'object') {
tempObj[key] = deepCopyObject(value);
} else {
tempObj[key] = value
}
}
}
return tempObj;
};
const resultData = deepCopyObject(data);
console.log("result", data)
You could create recursive function that takes data and old and new key do be added. It also checks if the some of the parent elements has the invalid key value at it ignores that object and its children.
const data = {"name":"A","invalid":{"distinctCountOnColumns":[{"key":"some_key","value":101557856}],"groupByAndCountOnColumns":[[{"key":"some_key","value":"no data","count":101557856}]]},"children":[{"name":"B","count":1654164,"children":[{"name":"B1","count":16564},{"name":"B2","count":165411}]},{"name":"C","count":15135}]}
function update(data, oldKey, newKey, isValid = true) {
if (oldKey in data && isValid) {
data[newKey] = data[oldKey]
}
for (let i in data) {
if (isValid) {
if (typeof data[i] == 'object') {
update(data[i], oldKey, newKey, isValid && i !== 'invalid')
}
}
}
}
update(data, 'count', 'value')
console.log(data)

Find and replace in nested objects

I'm struggling with the following issue:
I have a nested object. From the server I get a response with an object with changed values. So I want to find the object in my nested object and replace it.
My object has a structure like this:
$scope.page = {
id: 5,
label: 'myPage',
items : [
{
"type": "Container",
"id": 1,
"label": "header",
"items": [
{
"type": "Container",
"id": 2,
"label": "left",
"items": [
{
"type": "Menu",
"label": "settings-menu",
"id": "5"
},
{
"type": "Menu",
"label": "main-menu",
"id": "7"
}
]
},
{
"type": "Container",
"id": 4,
"label": "right",
"items": [
{
"type": "Post",
"label": "contact",
"id": "25"
}
]
}
]
},
{
"type": "Postlist",
"label": "nieuwsberichten",
"id": "17"
},
{
"type": "HTML",
"label": "over deze site",
"id": "18"
},
{
"type": "Other",
"label": "twitter feed",
"id": "19"
}
]
}
From the server I get a new object:
var newItem = {
"type": "Post",
"label": "contact",
"id": "25"
}
How can I update the object inside $scope.page the right way? I've tried the following:
$scope.findAndReplace(newItem,$scope.page.items);
$scope.findAndReplace = function(newItem, items) {
for (var i = 0; i < items.length; i++) {
if (items[i].id == newItem.id) {
items[i] = newItem;
} else if (items[i].items) {
$scope.findAndReplace(newItem, items[i].items);
}
}
}
and:
var oldItem = $scope.findById(item.id, $scope.page.items);
oldItem = newItem;
$scope.findById = function(id, items) {
var match = null;
angular.forEach(items, function(i){
if (match == null) {
if (i.id == id) {
match = i;
} else if (i.items) {
match = $scope.findById(id, i.items)
}
}
})
return match;
}
Neither of these options work. That's because of the nested loops where the object isn't the one in $scope.page anymore.
Anyone an idea to handle this?
Your example looks fine, can't understand why they are not working.
Neither of these options work. That's because of the nested loops where the object isn't the one in $scope.page anymore.
You can keep object reference by using angular.copy(newItem, oldItem)
Hi I have created a fiddle for you.
click for fiddle
for(var indx=0; indx < $scope.page.items.length; indx++) {
var tmpObj = $scope.page.items[indx];
if(tmpObj.hasOwnProperty('items')) {
// check inside
for(var indx1=0; indx1<tmpObj.items.length; indx1++ ) {
var innerObj = tmpObj.items[indx1];
// check for next level
if(innerObj.hasOwnProperty('items')) {
for(var counter=0; counter< innerObj.items.length; counter++) {
var thirdTmp = innerObj.items[counter];
console.log('3rd level inner object', thirdTmp);
if(thirdTmp.id === newItem.id) {
innerObj.items[counter] = newItem;
tmpObj.items[indx1] = innerObj;
$scope.page.items[indx] = tmpObj;
}
}
}
}
} else if(tmpObj.id === newItem.id) {
$scope.page.items[indx] = newItem;
}
};

How to check if a JSON key has unique values in a JSON file using javascript?

I have a JSON file like(for example) :
"fields": {
"asset": {
"values": [{
"asset": {
"id": "Info_text",
"type": "text",
"value": "ABCD"
}
},
{
"asset": {
"id": "Info_input",
"type": "input",
"value": "ABCDE"
}
}
]
}
}
How can I iterate over the values of "id" and check if they a unique or not in javascript?
here is complete demo without additional library, you can find if any key/value pair is unique in a json object and how many occurences were found :
var jsonData = {
"fields": [
{
"asset": {
"id": "Info_input",
"values": [
{
"asset": {
"id": "Info_text",
"type": "text",
"value": "ABCD"
}
},
{
"asset": {
"id": "Info_input",
"type": "input",
"value": "ABCDE"
}
},
{
"asset": {
"id": "Info_input",
"type": "input",
"value": "ABCDE"
}
}
]
}
}
]
}
function findKeyValueCount(key, value, obj) {
var count = 0;
var keys = Object.keys(obj);
keys.forEach(function(k) {
var v = obj[k];
if(typeof v === 'object') {
count += findKeyValueCount(key, value, v)
}
else if(k === key && v === value) {
count += 1;
}
});
return count;
}
function isUnique(key, value, obj) {
return findKeyValueCount(key, value, obj) === 1;
}
console.log(findKeyValueCount('id', 'Info_text', jsonData));
// -> 1
console.log(findKeyValueCount('id', 'Info_input', jsonData));
// -> 3
console.log(findKeyValueCount('value', 'ABCDE', jsonData));
// -> 2
console.log(findKeyValueCount('xxx', 'yyy', jsonData));
// -> 0
console.log(isUnique('id', 'Info_input', jsonData));
// -> false
console.log(isUnique('id', 'Info_text', jsonData));
// -> true
Have fun !

Categories