I am trying to convert the keys in JSON to the capital case using Javascript. I am successful to some extent. However, it is not creating the arrays in the correct way. It is inserting numbers before every object inside an array.
Input:
{
"id": "123",
"retweetCheck": {
"result": "OK",
"checks": [
{
"cId": "123"
},
{
"cId": "456"
}
]
},
"tweetCheck": {
"result": "OK",
"cId": "345",
"check": "Fail"
}
}
Code to convert the keys to capital case:
var responseContent = context.getVariable("response.content") || "";
responseContent = JSON.parse(responseContent) || "";
transformedCapitalizedObj = keysToCapitalCase(responseContent);
var finalResponseObj = {
Data: transformedCapitalizedObj
};
context.setVariable("response.content", JSON.stringify(finalResponseObj));
The function
function objEntries(obj) {
const keys = Object.keys(obj);
const keyValuePairs = keys.map(key => {
const value = obj[key];
return [key, value];
});
return keyValuePairs;
}
function keysToCapitalCase(objToProcess) {
if (!objToProcess || typeof objToProcess !== "object") return null;
var finalObj = {};
objToProcess = objEntries(objToProcess);
objToProcess.forEach(function (entry) {
var key = entry[0];
var value = entry[1];
key = key.charAt(0).toUpperCase() + key.slice(1);
if (typeof value == "object" || (value instanceof Array)) {
value = keysToCapitalCase(value);
}
finalObj[key] = value;
});
return finalObj;
}
The output I am getting currently is:
{
"Data":{
"RetweetCheck":{
"Checks":{
"0":{
"CId":"123"
},
"1":{
"CId":"456"
}
},
"Result":"OK"
},
"Id":"123",
"TweetCheck":{
"CId":"345",
"Check":"Fail",
"Result":"OK"
}
}
}
But ideally, the output should look like this:
{
"Data": {
"Id": "123",
"RetweetCheck": {
"Result": "OK",
"Checks": [
{
"CId": "123"
},
{
"CId": "456"
}
]
},
"TweetCheck": {
"Result": "OK",
"CId": "345",
"Check": "Fail"
}
}
}
It is basically inserting a serial number before each object inside an array instead of []. How this can be rectified. Any help will really do wonders.
When you call your function keysToCapitalCase(), first check if you have an array (with ES6, you can do this using Array.isArray()), and if you do, you can map the objects / inner arrays within that array to the result of recursively calling your keysToCapitalize function. Otherwise, if you get a standard object that isn't an array, you can perform your standard object mapping:
const obj = { "id": "123", "retweetCheck": { "result": "OK", "checks": [{ "cId": "123" }, { "cId": "456" } ] }, "tweetCheck": { "result": "OK", "cId": "345", "check": "Fail" } };
function keysToCapitalCase(objToProcess) {
if (!objToProcess || typeof objToProcess !== "object") return null;
if(Array.isArray(objToProcess)) {
return objToProcess.map(obj => keysToCapitalCase(obj));
}
var finalObj = {};
objToProcess = Object.entries(objToProcess); // if you can support it, `Object.entries()` does what `objEntries` does
objToProcess.forEach(function(entry) {
var key = entry[0];
var value = entry[1];
key = key.charAt(0).toUpperCase() + key.slice(1);
if (typeof value == "object") {
value = keysToCapitalCase(value);
}
finalObj[key] = value;
});
return finalObj;
}
var finalResponseObj = {Data: keysToCapitalCase(obj)};
console.log(finalResponseObj);
I would probably write the above method in a similar way, but instead using some inbuilt functions to make it a little more concise, such as .map() and Object.fromEntries():
const obj = { "id": "123", "retweetCheck": { "result": "OK", "checks": [{ "cId": "123" }, { "cId": "456" } ] }, "tweetCheck": { "result": "OK", "cId": "345", "check": "Fail" } };
const cap = str => str.charAt(0).toUpperCase() + str.slice(1);
const keysToCapitalCase = (objToProcess) => {
if (Object(objToProcess) !== objToProcess) return null;
return Array.isArray(objToProcess)
? objToProcess.map(obj => keysToCapitalCase(obj))
: Object.fromEntries(Object.entries(objToProcess).map(([key, val]) => [
cap(key), Object(val) === val ? keysToCapitalCase(val) : val
]));
}
const finalResponseObj = {Data: keysToCapitalCase(obj)};
console.log(finalResponseObj);
As indicated by the {} brackets, instead of the wanted [] brackets you are creating an empty object and not an Array.
To create an Array just change your var finalObj = {}; to var finalObj = [];
with an Array finalObj[key] = value will no longer work, you will now have to use finalObj.push(value)
Related
I am accessing JSON which looks like this.
[
{
"itemType": "SelectionTitle",
"_id": "5ada2217c114ca048e1db9b0",
"created_by": "5ab57289d8d00507b29a3fdd",
"selectionFile": {
"item.Type": "SelectionFile",
"name": "1105F.MID",
"active": true,
"isFactory.Default": false,
"selection.Type": "Music",
"sfzFile": "",
"destination": "/data/uploads",
"encoding": "7bit",
"fieldname": "file",
"filename": "782f49a7cd72b865b4e2d286816792e7"
...
}
}, ...
And I am having trouble renaming the object keys which have the . in the name to an _. For example:
item.Type or selection.Type to item_Type or selection_Type.
This is what I am trying to use:
var json = jsonFromExampleAbove;
str = JSON.stringify(json);
str = str.selectionFile.replace(/\".\":/g, "\"_\":");
json = JSON.parse(str);
console.log(json);
I am getting a console log error. I think it is because the values I am trying to replace are nested, but not sure. I am still very much a beginner here.
Thank you.
I would be tempted to do it with a bit of recursion using Object.entries and Object.fromEntries
const input = [
{
"itemType": "SelectionTitle",
"_id": "5ada2217c114ca048e1db9b0",
"created_by": "5ab57289d8d00507b29a3fdd",
"selectionFile": {
"item.Type": "SelectionFile",
"name": "1105F.MID",
"active": true,
"isFactory.Default": false,
"selection.Type": "Music",
"sfzFile": "",
"destination": "/data/uploads",
"encoding": "7bit",
"fieldname": "file",
"filename": "782f49a7cd72b865b4e2d286816792e7"
}
}];
function replaceDots(obj){
return Object.fromEntries(Object.entries(obj).map( ([key,value]) => {
const newKey = key.replace(".","_");
return typeof value == "object"
? [newKey, replaceDots(value)]
: [newKey, value]
}));
}
const result = input.map(replaceDots);
console.log(result);
Your solution with a replaceAll on the stringified JSON could result in unwanted problems when a value contains a dot.
For renaming nested keys of an object you need go over the object recursively and assign the new keys while removing the old ones
// for ES6
function renameKey(obj) {
// loop over all keys
for (let k in obj) {
// apply recursively if value is an object
if (typeof obj[k] === "object" && obj[k] !== null)
renameKey(obj[k]);
// only replace keys of type String
if (typeof k === 'string' || k instanceof String) {
const newKey = k.replaceAll(".", "_");
// assign {newKey:value} to object and delete old key
delete Object.assign(obj, { [newKey]: obj[k] })[k];
}
}
return obj
}
You can use a recursive approach to replace the keys in the input object(s) with your desired result. There's no need to use JSON.stringify(), one can simply iterate through the objects.
We'll use searchValue to specify what we wish to search for and newvalue to specify the replacement, in this case they will be /./g and '_'.
let arr = [
{
"itemType": "SelectionTitle",
"_id": "5ada2217c114ca048e1db9b0",
"created_by": "5ab57289d8d00507b29a3fdd",
"selectionFile": {
"item.Type": "SelectionFile",
"name": "1105F.MID",
"active": true,
"isFactory.Default": false,
"selection.Type": "Music",
"sfzFile": "",
"destination": "/data/uploads",
"encoding": "7bit",
"fieldname": "file",
"filename": "782f49a7cd72b865b4e2d286816792e7"
}
}
]
function replaceKeys(obj, searchvalue, newvalue, newObj) {
if (!newObj) newObj = Array.isArray(obj) ? []: {};
for(let k in obj) {
let newKey = k.replace(searchvalue, newvalue);
if (typeof(obj[k]) === 'object') {
newObj[newKey] = Array.isArray(obj[k]) ? []: {};
replaceKeys(obj[k], searchvalue, newvalue, newObj[newKey])
} else {
newObj[newKey] = obj[k];
}
}
return newObj;
}
const result = replaceKeys(arr, /\./g, '_');
console.log('Result:', result);
I have two nested JSON objects
//Json object1
{
"version": "1",
"user": {
"id": 123
}
}
//Json object2 =
{
"version": "1",
"user": {
"i": 123
}
}
1)It should only check for keys and not values during comparison.
So above should return false and below should return true
2) It should also follow the structure say user.id is not the same as just id.
//Json object1
{
"version": "1",
"user": {
"id": 123
}
}
//Json object2
{
"version": "1",
"user": {
"id": 12
}
}
I have tried the code below but to no avail
exports.compareObjects = async(model, response) => {
switch (Object.prototype.toString.call(model)) {
case '[object]':
var x;
var mKeys = Object.keys(model);
for (x in mKeys) {
return this.compareObjects(Object.keys(model)[x], Object.keys(response)[x]);
}
break;
case '[object Object]':
var x1;
var mKeys1 = Object.keys(model);
for (x1 in mKeys1) {
return this.compareObjects(Object.keys(model)[x1], Object.keys(response)[x1]);
}
break;
case '[object Array]':
return this.compareObjects(model[0], response[0]);
// case '[object String]':
// return model === response;
default:
return true;
}
};
This uses a recursive key search to build out an array of keys in each object you want to compare.
It tests fairly strict equality (no extra keys in either object)
let obj1 = JSON.parse(`{
"version": "1",
"user": {
"id": 123
}
}`);
let obj2 = JSON.parse(`{
"version": "1",
"user": {
"i": 123
}
}`);
let obj3 = JSON.parse(`{
"version": "1",
"user": {
"id": 123
}
}`);
let obj4 = JSON.parse(`{
"version": "1",
"user": {
"id": 12
}
}`);
let test1 = structureIsEqual(obj1, obj2);
let test2 = structureIsEqual(obj3, obj4);
console.log('Structure of first two match: ' + test1);
console.log('Structure of second two match: ' + test2);
function structureIsEqual(obj1, obj2) {
let tree1 = getKeys(obj1).sort();
let tree2 = getKeys(obj2).sort();
if(tree1.length !== tree2.length)
return false;
let mismatch = tree1.find((x, idx) => tree2[idx] !== x);
return !mismatch;
}
function getKeys(obj) {
return recursiveKeys(obj, [], []);
}
function recursiveKeys(obj, result, todo, root = '') {
Object.keys(obj).forEach(key => {
if(typeof obj[key] === 'object') {
result.push(root + key);
todo.push({ obj: obj[key], root: root + key + '.' });
} else {
result.push(root + key);
}
});
if(todo.length > 0) {
let todoItem = todo.pop();
return recursiveKeys(todoItem.obj, result, todo, todoItem.root);
}else {
return result;
}
}
Could be you could fix it by this line
return this.compareObjects(Object.keys(model)[x1], Object.keys(response)[x1]);
Instead do
return this.compareObjects(model[x1], response[x1]);
I would do it like this
1. you only need to compare objects (all other dont have keys)
2. recurse if value is also object
function compare(obj,model){
let keys=Object.keys(model)
let thisLevelOK=true
for (let key in keys.length){
if (obj[key]===undefined){
thisLevelOK=false
break
} else if (model[key].toString()=="[object Object]" && compare(obj[key],model[key])==false){
thisLevelOK=false
break
}
}
//in case obj has more keys then model
thisLevelOK=thisLevelOK && compare(model,obj)
return thisLevelOK
}
var compareObjects = (model, response) => {
switch (Object.prototype.toString.call(model)) {
case '[object]':
var x;
var mKeys = Object.keys(model);
for (x in mKeys) {
return this.compareObjects(Object.keys(model)[x], Object.keys(response)[x]);
}
break;
case '[object Object]':
var x1;
var mKeys1 = Object.keys(model);
for (x1 in mKeys1) {
let t = this.compareObjects(Object.keys(model)[x1], Object.keys(response)[x1]);
if(!t){
return false
}
if(typeof model[mKeys1[x1]] == "object"){
return this.compareObjects(model[mKeys1[x1]], response[mKeys1[x1]])
}
}
case '[object Array]':
return this.compareObjects(model[0], response[0]);
case '[object String]':
return model === response;
default:
return true;
}
};
let a = {
"version": "1",
"user": {
"id": 123,
"n":"d",
"j":{
"ns":"m"
}
}
}
let b = {
"version": "1",
"user": {
"id": 123,
"n":"d",
"j":{
"ns":"m"
}
}
}
var t = compareObjects(a,b)
console.log(t)
Given an empty JSON 1:
JSON1 = {
"person": { "firstName": "" },
"products": { "packName": "", "packSize": "" }
}
and JSON 2 which has more fields than JSON 1:
JSON2 = {
"person": { "firstName": "Ahmed", "job": "Doctor" },
"products": { "packName": "antibiotic", "packSize": "large" }
}
I want to fill JSON 1 with its corresponding values in JSON 2
{
"person": { "firstName": "Ahmed" },
"products": { "packName": "antibiotic", "packSize": "large" }
}
I have tried several methods but not getting there
var newObj = {};
var parsedJson1 = JSON.parse(tw.local.JSON1);
var parsedJson2 = JSON.parse(tw.local.JSON2);
var i;
for (i in parsedJson1) {
var key=i;
var subkey=i;
for (j in parsedJson2) {
var k=j;
var s=j;
if (key == k && subkey == s) {
newObj[key][subkey] = parsedJson2[j];
}
}
}
tw.local.stringifiedJSON = JSON.stringify(newObj);
Here's an example on how to do it:
Note that this assumes the following:
you know the second object will have all required keys.
all values at the first level are objects
note that this solution will work only for the specified structure, and if you need to handle any variations you will need to make it more defensive.
Steps:
iterate over the valid keys (all keys on JSON1)
for each valid key, add it to newObj and iterate over valid subKeys
copy values from JSON2 to newObj
Note: I changed your code a bit so it can be executed on this site
const JSON1 = `{
"person": { "firstName": "" },
"products": { "packName": "", "packSize": "" }
}`;
const JSON2 = `{
"person": { "firstName": "Ahmed", "job": "Doctor" },
"products": { "packName": "antibiotic", "packSize": "large" }
}`;
const parsedJson1 = JSON.parse(JSON1);
const parsedJson2 = JSON.parse(JSON2);
const newObj = {};
const validKeys = Object.keys(parsedJson1);
for (let i of Object.keys(parsedJson1)) {
if (newObj[i] === undefined) {
newObj[i] = {};
}
for (let j of Object.keys(parsedJson1[i])) {
newObj[i][j] = parsedJson2[i][j];
}
}
console.log(JSON.stringify(newObj));
You should use recursion to traverse target object for robustness. I formatted code for better readability.
function getNode(obj, path) {
var ret = obj;
try {
// replace forEach with for-loop for browser compatibilities
path.forEach(function (p) {
ret = ret[p];
});
} catch (e) {
return undefined;
}
return ret;
}
function fillJSONWithAnother(target, source, path) {
if (!(target && source)) return target;
// Assign path as an empty array for first call
path = path || [];
// Get target node and source node for comparing
var targetValue = getNode(target, path);
var sourceValue = getNode(source, path);
// targetValue is not a leaf node (i.e. is Object), should traverse its children nodes
if (targetValue && typeof targetValue === "object") {
for (var key in targetValue) {
fillJSONWithAnother(target, source, path.concat([key]));
}
}
// targetValue is a leaf node (i.e. not Object) and source value exists, set corresponding value
else if (sourceValue !== undefined) {
var node = getNode(target, path.slice(0, path.length - 1));
node[path[path.length - 1]] = sourceValue;
}
return target;
}
// Test case
var obj1 = {
"person": {
"firstName": ""
},
"products": {
"packName": "",
"packSize": "l"
}
};
var obj2 = {
"person": {
"firstName": "Ahmed",
"job": "Doctor"
},
"products": {
"packName": "antibiotic",
"packSize": "large"
}
}
fillJSONWithAnother(obj1, obj2);
// {"person":{"firstName":"Ahmed"},"products":{"packName":"antibiotic","packSize":"large"}}
I have a JSON object that is returned in different ways, but always has key. How can I get it?
E.g.
"Records": {
"key": "112"
}
Or
"Records": {
"test": {
"key": "512"
}
}
Or even in array:
"Records": {
"test": {
"test2": [
{
"key": "334"
}
]
}
}
Tried several options, but still can't figure out (
I will not write the code for you but give you an idea may be it will help, First convert JSON object in to string using
JSON.stringify(obj);
after that search for Key using indexOf() method.
Extract previous '{' and Next '}' string and again cast in to JSON object.
using
var obj = JSON.parse(string);
Then
var value = obj.key
I think this migth be solution (asuming key is always string and you don't care about res of data)
const data = [`"Records": {
"test": {
"test2": [
{
"key": "334",
"key": "3343"
}
]
}
}`, `"Records": {
"test": {
"key": "512"
}
}`, `"Records": {
"key": "112"
}`]
const getKeys = data => {
const keys = []
const regex = /"key"\s*:\s*"(.*)"/g
let temp
while(temp = regex.exec(data)){
keys.push(temp[1])
}
return keys
}
for(let json of data){
console.log(getKeys(json))
}
How can i get it?
Recursively! e.g.
function getKey(rec) {
if (rec.key) return rec.key;
return getKey(rec[Object.keys(rec)[0]]);
}
https://jsfiddle.net/su42h2et/
You could use an iterative and recursive approach for getting the object with key in it.
function getKeyReference(object) {
function f(o) {
if (!o || typeof o !== 'object') {
return;
}
if ('key' in o) {
reference = o;
return true;
}
Object.keys(o).some(function (k) {
return f(o[k]);
});
}
var reference;
f(object);
return reference;
}
var o1 = { Records: { key: "112" } },
o2 = { Records: { test: { key: "512" } } },
o3 = { Records: { test: { test2: [{ key: "334" }] } } };
console.log(getKeyReference(o1));
console.log(getKeyReference(o2));
console.log(getKeyReference(o3));
You can try this
const data = {
"Records": {
"key": "112"
}
};
const data2 = {
"Records": {
"test": { "key": "512" }
}
};
const data3 = {
"Records": {
"test": {
"test2": [
{ "key": "334" },
]
}
}
};
function searchKey(obj, key = 'key') {
return Object.keys(obj).reduce((finalObj, objKey) => {
if (objKey !== key) {
return searchKey(obj[objKey]);
} else {
return finalObj = obj[objKey];
}
}, [])
}
const result = searchKey(data);
const result2 = searchKey(data2);
const result3 = searchKey(data3);
console.log(result);
console.log(result2);
console.log(result3);
Here is what I want to do:
I have a tree (javascript object-literal structure) with multiple levels.
I have a value of a particular key of this object.
I want to search for this exact key-value pair in the structure and return the value of another key as an output.
For clarity following is my object literal:
{
"nodeId": 1081,
"appId": 150,
"displayText": "Welcome here",
"Nodes": [
{
"nodeId": 2000,
"appId": 150,
"displayText": "Buy",
"parentNodeId": 1081,
"Nodes": [
{
"nodeId": 2003,
"appId": 150,
"displayText": "tCars",
"parentNodeId": 2000,
"Nodes": [
{
"nodeId": 2006,
"appId": 150,
"displayText": "Diesel",
"parentNodeId": 2003,
"Nodes": [
{
"nodeId": 2008,
"appId": 150,
"displayText": "Price", //This is what I want as return value.
"parentNodeId": 2006,
"Nodes": [],
"nodeCode": "RN_1_1_2_1_3_2_4_1",
"parentCode": "RN_1_1_2_1_3_2",
"jumpToNode": "RN_1_1" //here is the value that I have with me.
}
],
"nodeCode": "RN_1_1_2_1_3_2",
"parentCode": "RN_1_1_2_1"
}
],
"concatWithHeader": false,
"nodeCode": "RN_1_1_2_1",
"parentCode": "RN_1_1"
}
],
"nodeCode": "RN_1_1",
"parentCode": "RN"
}
],
"nodeCode": "RN",
"parentCode": "ROOT_NODE"
}
2. Value that I have with me is "RN_1_1" against jumpToNode
3. I want to search in this object literal and get the value of the key displayText
I searched and tried few things for this but couldnt get the logic to iterate over the inner Nodes objects.
Method I wrote so far:
function getObjects(tree){
var searchkey="RN_1_1";
var displayText = "displayText";
var nodeCode = "nodeCode";
var returnText;
if (tree.hasOwnProperty(nodeCode)) {
var obj = tree[nodeCode];
if(obj == searchkey){
returnText = tree[displayText]; //gives me the return text
break;
}
else{
//here I should iterate over the inner `Nodes` and get the required value.
}
}
}
Please help.
Thanks.
I think you can do something like this which works recursively:
function findProperty(obj, prop, val, propToFetch) {
var answer;
if (obj.hasOwnProperty(prop) && obj[prop] === val) {
return obj[propToFetch];
}
for (var i = 0, len = obj.Nodes.length; i < len; i++) {
answer = findProperty(obj.Nodes[i], prop, val, propToFetch);
if (answer !== null) {return answer;}
}
return null;
}
var result = findProperty(data, "jumpToNode", "RN_1_1", "displayText");
Working demo here: http://jsfiddle.net/jfriend00/EjC5V/
Accordingly to your JSON object you can use this way:
var searchKey="RN_1_1",
displayText = "displayText",
nodeCode = "nodeCode",
returnText,
treeSearch = function (obj, searchKey) {
if (obj[nodeCode] === searchKey) {
returnText = obj[displayText];
} else {
if (obj['Nodes'][0]) {
treeSearch(obj['Nodes'][0], searchKey);
} else {
returnText = null
}
}
};
treeSearch(JSONdata, 'RN_1_1_2_1_3_2');
I have flattened the array using the nodeId to be easier to search through it.
After you flatten the array you can filter it as you wish(i suggest using underscorejs.org)
Here is the live example. The result is displayed in the console.
function flattenNodes (obj, newArr) {
if (obj && obj.Nodes) {
var nodes = obj.Nodes;
delete(obj.Nodes);
newArr[obj.nodeId] = obj;
return flattenNodes(nodes.pop(), newArr);
} else {
return newArr;
}
};
var flattenArr = flattenNodes(arr, new Array());
function findInJumpToNode(find) {
for(key in flattenArr) {
if (flattenArr[key] && flattenArr[key]['jumpToNode']) {
if (flattenArr[key]['jumpToNode'] == find) {
return flattenArr[key];
}
}
}
}
var found = findInJumpToNode('RN_1_1');
console.log(found);
You can use recursion to handle your case.
Check out this sample on jsFiddle.
var nodes= [getNodes()];
alert(getObjects(nodes));
function getObjects(tree){
var searchkey="RN_1_1_2_1_3_2_4_1";
var displayText = "displayText";
var nodeCode = "nodeCode";
var returnText;
if(tree.length > 0)
{
if(tree[0]["nodeCode"] === searchkey)
{
return tree[0][displayText];
}
if(typeof tree[0]["Nodes"] === "undefined")
{
return;
}
return getObjects(tree[0]["Nodes"]);
}
}