Javascript: Find key and its value in JSON - javascript

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);

Related

How to get the parent object value using child value

I have value "b" with me and I want to get the item "3648" using that how can I do that using JavaScript?
{
"someID": "16385421",
"items": {
"9836": {
"id": "a"
},
"3648": {
"id": "b"
},
"7738": {
"id": "c"
}
}
}
o/p
"3648": {
"id": "b"
}
Edited after your comment:
getByItemId = ( val ) => {
let result;
for( let elem in data.items ){
const check = data.items[ elem ];
if( check.id == val ) result = check;
}
console.log( result )
return result;
}
getByItemId( "b" )
You could do it with some code that resembled the following.
First we get all of the object keys in the obj.items object.
Next we iterate through them.
At each step, we check to see if the desired ID is contained within the sub-object indexed by the current key in the array. If it's found, we update the result object.
We then return an empty object if the key isn't present, and something like the following if it was found: {key: '3648', id: 'b'}.
"use strict";
function byId(id){return document.getElementById(id)}
function qs(sel,parent=document){return parent.querySelector(sel)}
function qsa(sel,parent=document){return parent.querySelectorAll(sel)}
window.addEventListener('load', onLoaded, false);
var rawData = {
"someID": "16385421",
"items": {
"9836": {
"id": "a"
},
"3648": {
"id": "b"
},
"7738": {
"id": "c"
}
}
};
function getIDs(data, searchItemId)
{
let keys = Object.keys(data.items);
let result = {};
keys.forEach( keyFunc );
return result;
function keyFunc(elem, index, collection)
{
if (data.items[elem].id == searchItemId)
{
result.key = elem;
result.id = searchItemId;
}
}
}
function onLoaded(evt)
{
console.log( getIDs(rawData, "b") );
}
You can do:
const obj = {
someID: '16385421',
items: {
9836: {
id: 'a',
},
3648: {
id: 'b',
},
7738: {
id: 'c',
},
},
}
const getParentObjectByValue = (obj, value) => {
const key = Object.keys(obj.items).find((k) => obj.items[k].id === value)
return key ? { [key]: obj.items[key].id } : undefined
}
console.log(getParentObjectByValue(obj, 'b'))
console.log(getParentObjectByValue(obj, 'zz'))

Search multiple elements by key inside multiple json structures

I need to get the values of all the the arrays matching the items key. I'm making a script in Javascript that needs to read the objects inside multiple items arrays in multiple json files, but each json has a different structure. Example:
file1.json:
{
"name":"First file",
"randomName3874":{
"items":[
{
"name":"item1"
}
]
},
"items":[
{
"name":"randomItem2"
}
]
}
file2.json
{
"name":"Another file",
"randomName00000":{
"nestedItems":{
"items":[
{
"name":"item87"
}
]
}
},
"stuff":{
"items":[
{
"name":"randomItem35"
}
]
}
}
Desired result:
{
"data":[
{
"items":[
{
"name":"item1"
}
]
},
{
"items":[
{
"name":"randomItem2"
}
]
},
{
"items":[
{
"name":"item87"
}
]
},
{
"items":[
{
"name":"randomItem35"
}
]
}
]
}
In both files I want to extract the arrays that have the key items. In the examples above the script should find 4 arrays. As you can see in both files each array is nested differently. How can I make this using Javascript?
This will do it:
function omit(key, obj) {
const { [key]: omitted, ...rest } = obj;
return rest;
}
function getItems(obj) {
return (typeof obj === 'object'
? 'items' in obj
? [{ items: obj.items }].concat(getItems(omit('items', obj)))
: Object.values(obj).map(v => getItems(v))
: []
).flat()
}
console.log({
data: [file1, file2].map(o => getItems(o)).flat()
})
See it working:
const file1 = {
"name":"First file",
"randomName3874":{
"items":[
{
"name":"item1"
}
]
},
"items":[
{
"name":"randomItem2"
}
]
}
const file2 = {
"name":"Another file",
"randomName00000":{
"nestedItems":{
"items":[
{
"name":"item87"
}
]
}
},
"stuff":{
"items":[
{
"name":"randomItem35"
}
]
}
}
function omit(key, obj) {
const { [key]: omitted, ...rest } = obj;
return rest;
}
function getItems(obj) {
return (typeof obj === 'object'
? 'items' in obj
? [{ items: obj.items }].concat(getItems(omit('items', obj)))
: Object.values(obj).map(v => getItems(v))
: []
).flat()
}
console.log({
data: [file1, file2].map(o => getItems(o)).flat()
})
Let's take it a step further and make it generic (work with an array of objects and extract any key) and provide it as a function, which you can use in other projects, as well:
function extractKey(objects, key) {
const omit = (key, obj) => {
const { [key]: omitted, ...rest } = obj;
return rest;
}
const getValues = (obj) => (typeof obj === 'object'
? key in obj
? [{ [key]: obj[key] }].concat(getValues(omit(key, obj)))
: Object.values(obj).map(o => getValues(o))
: []
).flat();
return objects.map(o => getValues(o)).flat()
}
// use:
extractKey([file1, file2], 'items');
See it working:
function extractKey(objects, key) {
const omit = (key, obj) => {
const { [key]: omitted, ...rest } = obj;
return rest;
}
const getValues = (obj) => (typeof obj === 'object'
? key in obj
? [{ [key]: obj[key] }].concat(getValues(omit(key, obj)))
: Object.values(obj).map(o => getValues(o))
: []
).flat();
return objects.map(o => getValues(o)).flat()
}
// test:
const file1 = {
"name":"First file",
"randomName3874":{
"items":[
{
"name":"item1"
}
]
},
"items":[
{
"name":"randomItem2"
}
]
}
const file2 = {
"name":"Another file",
"randomName00000":{
"nestedItems":{
"items":[
{
"name":"item87"
}
]
}
},
"stuff":{
"items":[
{
"name":"randomItem35"
}
]
}
}
console.log(
{ data: extractKey([file1, file2], 'items') }
)
Looping over like a tree-nested loop should do it.
let file1 = {
"name": "First file",
"randomName3874": {
"items": [
{
"name": "item1"
}
]
},
"items": [
{
"name": "randomItem2"
}
]
}
let file2 = {
"name": "Another file",
"randomName00000": {
"nestedItems": {
"items": [
{
"name": "item87"
}
]
}
},
"stuff": {
"items": [
{
"name": "randomItem35"
}
]
}
}
let itemsValues = [];
let desiredKey = 'items'
let loop = (value) => {
if (Array.isArray(value)) {
value.forEach(loop);
} else if (typeof value === 'object' && value !== null) {
Object.entries(value).forEach(([key, val]) => (key === desiredKey) ? itemsValues.push({ [desiredKey]: val }) : loop(val));
}
}
loop(file1);
loop(file2);
console.log(itemsValues);
This should work:
function getIdInObjects(id, objects, output = { data: [] }) {
if (id in objects) output.data.push({[id]: objects[id]});
for (const key in objects) {
if (typeof(objects[key]) === 'object') getIdInObjects(id, objects[key], output);
}
return output;
}
console.log('items', [object1, object2])

Convert keys from key value pair to capital case using javaScript

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)

How to iterate nested object keys with child nested

I want to iterate the nested object keys which will be have inner child nested as well:
Nested object code:
{
"F2C3C496-BEA6-A5E8-15F0-E2867304B463": {
"D39FD497-9529-6CC3-70DE-E8D9277C18D3": {
"child": {
"87A1817D-CFA9-70FD-131B-224658AF13CE": {
"next": {
"F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A": {}
}
}
},
"next": {
"B49927ED-369E-E219-FC1A-8E4BAAFC3933": {
"next": {}
}
}
}
}
}
JS code to iterate:
flowThrough = (obj, callback, context?, path?: string) => {
let nestedKey=';'
Object.keys(obj).forEach(key => {
if(!isEmptyObject(key))
callback(key, context, path);
if (!isEmpty(obj[key]) && typeof obj[key] === 'object') {
if(obj[key].hasOwnProperty('next'))
nestedKey = obj[key].next;
else if(obj[key].hasOwnProperty('child'))
nestedKey = obj[key].child;
else nestedKey = obj[key];
this.flowThrough(nestedKey, callback, context, (path)? has(context, path.concat(".next"))?path.concat(`.next[${get(context, path.concat(".next").length)}]`): path.concat('.').concat("next[0]") : key)
}
})
}
Actually, the above code working for one nested level to get key(ids). If it reaches the empty object key then the loop is ending there. actually, it should check any other child/next is there or not.
Expected output:
{ "flow": [
{
"id": "F2C3C496-BEA6-A5E8-15F0-E2867304B463",
"next": [
{
"id": "D39FD497-9529-6CC3-70DE-E8D9277C18D3",
"child": [
{
"id": "87A1817D-CFA9-70FD-131B-224658AF13CE",
"next": [
{
"id": "F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A",
"next": []
}
]
}
],
"next": [
{
"id": "B49927ED-369E-E219-FC1A-8E4BAAFC3933",
"next": []
}
]
}
]
}
]
}
Please give me the solution for this.
let obj = {
"F2C3C496-BEA6-A5E8-15F0-E2867304B463": {
"D39FD497-9529-6CC3-70DE-E8D9277C18D3": {
"child": {
"87A1817D-CFA9-70FD-131B-224658AF13CE": {
"next": {
"F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A": {}
}
}
},
"next": {
"B49927ED-369E-E219-FC1A-8E4BAAFC3933": {
"next": {}
}
}
}
}
}
const setObj = e => {
let tmp = {}
if (Array.isArray(e[0])) e = e[0]
if (typeof e[0] === 'string' && e[0].split('-').length === 5) {
tmp = {
id: e[0],
next: setObj(Object.entries(e[1]))
};
} else if (e[1]) {
tmp = {
child: setObj(Object.entries(e[1]))
};
}
return tmp
}
let newobj = Object.entries(obj).map(setObj)[0]
console.log(newobj)
Original answer...
Here's one way to go about it. Use a recursive function along with Object.entries to gather the data. I found that the result was a series of nested arrays of id's - so I flattened them all with join() and then split them again. Finally, filter() helped remove the empty array indexes
let obj = {
"F2C3C496-BEA6-A5E8-15F0-E2867304B463": {
"D39FD497-9529-6CC3-70DE-E8D9277C18D3": {
"child": {
"87A1817D-CFA9-70FD-131B-224658AF13CE": {
"next": {
"F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A": {}
}
}
},
"next": {
"B49927ED-369E-E219-FC1A-8E4BAAFC3933": {
"next": {}
}
}
}
}
}
const getKeys = e => {
let ret = []
if (e[0].split('-').length === 5) ret.push(e[0]);
if (e[1]) ret.push(Object.entries(e[1]).map(getKeys))
return ret.join(',')
}
let keys = Object.entries(obj).map(getKeys)[0].split(",").filter(e => e)
console.log(keys)

how to change value of all nodes of json array

I have a json array with different key values and need to add a ServerUrl to the beginning of all node values using a loop without writing multiple statements to do that by using javascript:
"Urls": [
{ "getCar": "/getAllCars" },
{ "getPerson": "/getAllPersons" },
{ "getBook": "/getAllBooks" }
],
"ServerUrl": "http://192.168.1.1:3000"
The expected result must be:
"Urls": [
{ "getCar": "http://192.168.1.1:3000/getAllCars" },
{ "getPerson": "http://192.168.1.1:3000/getAllPersons" },
{ "getBook": "http://192.168.1.1:3000/getAllBooks" }
],
Any advice would be appreciated.
You can use map to map your objects to new objects. Those objects have a single property, which you can get with Object.keys. The new object can get that same property name using the computed property name feature:
var obj = {
"Urls": [
{ "getCar": "/getAllCars" },
{ "getPerson": "/getAllPersons" },
{ "getBook": "/getAllBooks" }
],
"ServerUrl": "http://192.168.1.1:3000"
};
var urls = obj.Urls.map(o => Object.keys(o).map(k => ({ [k]: obj.ServerUrl + o[k] }))[0]);
console.log(urls);
const jsonVal = {
"Urls": [
{ "getCar": "/getAllCars" },
{ "getPerson": "/getAllPersons" },
{ "getBook": "/getAllBooks" }
],
"ServerUrl": "http://192.168.1.1:3000"
}
const result = jsonVal.Urls.map(val =>
Object.keys(val).reduce((resultObj, endpointKey) => {
resultObj[endpointKey] = `${jsonVal.ServerUrl}${val[endpointKey]}`;
return resultObj;
}, {})
);
Try (where your data are in d)
d.Urls.forEach( (x,i,a,k=Object.keys(x)[0]) => x[k] = d.ServerUrl + x[k]);
let d = {
"Urls": [
{ "getCar": "/GetAllGroupCustomers" },
{ "getPerson": "/getAllItems" },
{ "getBook": "/GetAllCustomers" }
],
"ServerUrl": "http://192.168.1.1:3000"
}
d.Urls.forEach( (x,i,a,k=Object.keys(x)[0]) => x[k] = d.ServerUrl + x[k]);
console.log(d);
A version that modifies your own object
var obj = {
"Urls": [
{ "getCar": "/getAllCars" },
{ "getPerson": "/getAllPersons" },
{ "getBook": "/getAllBooks" }
],
"ServerUrl": "http://192.168.1.1:3000"
};
obj.Urls.forEach(o => o[Object.keys(o)[0]] = `${obj.ServerUrl}${o[Object.keys(o)[0]]}`);
console.log(obj);

Categories

Resources