I have to search for a key in nested JSON by JavaScript or by jQuery. In my JSON object all the keys are unique. I tried some solutions myself but they did not work. Here is my code:
json = {
"app": [{
"Garden": {
"Flowers": {
"Red flower": "Rose",
"White Flower": "Jasmine",
"Yellow Flower": "Marigold"
}
},
"Fruits": {
"Yellow fruit 1": "Mango",
"Green fruit 2": "Guava",
"White Flower 3": "groovy"
},
"Trees": {
"label": {
"Yellow fruit 2": [{"type a":"Pumpkin", "type b": "Banana",..}],
"White Flower 2": ["Bogan 1", "Bogan 2", ...]
}
}],...
}
How can I search for a specific key in given object?
If I pass lookup(json, "type a")
it should return "Pumpkin"
, OR If I search for "White Flower 2"
it should return ["Bogan 1", "Bogan 2", ...]
Here is my try, which is not working:
function lookup(obj, k){
for (key in obj){
value = obj[key];
if (k == key) return [k, value];
if (type(value) == "Object"){
var y = lookup(value, k);
if (y && y[0]== k)return y;
}
if(type(value) == "Array"){
for (i in value)
{
var x = lookup(value[i], k);
if (x && x[0]== k)return x;
}
}
console.log(key, value);
return null;
}
}
To find the type of the object, I'm using this code:
function type(object){
var stringConstructor = "test".constructor;
var arrayConstructor = [].constructor;
var objectConstructor = {}.constructor;
if (object === null) {
return "null";
}
else if (object === undefined) {
return "undefined";
}
else if (object.constructor === stringConstructor) {
return "String";
}
else if (object.constructor === arrayConstructor) {
return "Array";
}
else if (object.constructor === objectConstructor) {
return "Object";
}
else {
return "null";
}
}
I have to search for a key in nested JSON by JavaScript or by jQuery. In my JSON object all the keys are unique. I tried some solutions myself but they did not work. Here is my code:
json = {
"app": [{
"Garden": {
"Flowers": {
"Red flower": "Rose",
"White Flower": "Jasmine",
"Yellow Flower": "Marigold"
}
},
"Fruits": {
"Yellow fruit 1": "Mango",
"Green fruit 2": "Guava",
"White Flower 3": "groovy"
},
"Trees": {
"label": {
"Yellow fruit 2": [{"type a":"Pumpkin", "type b": "Banana",..}],
"White Flower 2": ["Bogan 1", "Bogan 2", ...]
}
}],...
}
How can I search for a specific key in given object?
If I pass lookup(json, "type a")
it should return "Pumpkin"
, OR If I search for "White Flower 2"
it should return ["Bogan 1", "Bogan 2", ...]
Here is my try, which is not working:
function lookup(obj, k){
for (key in obj){
value = obj[key];
if (k == key) return [k, value];
if (type(value) == "Object"){
var y = lookup(value, k);
if (y && y[0]== k)return y;
}
if(type(value) == "Array"){
for (i in value)
{
var x = lookup(value[i], k);
if (x && x[0]== k)return x;
}
}
console.log(key, value);
return null;
}
}
To find the type of the object, I'm using this code:
function type(object){
var stringConstructor = "test".constructor;
var arrayConstructor = [].constructor;
var objectConstructor = {}.constructor;
if (object === null) {
return "null";
}
else if (object === undefined) {
return "undefined";
}
else if (object.constructor === stringConstructor) {
return "String";
}
else if (object.constructor === arrayConstructor) {
return "Array";
}
else if (object.constructor === objectConstructor) {
return "Object";
}
else {
return "null";
}
}
Share
Improve this question
edited Aug 6, 2016 at 14:29
Paul Roub
36.4k27 gold badges85 silver badges94 bronze badges
asked Aug 6, 2016 at 14:08
LaxmikantLaxmikant
2,2164 gold badges32 silver badges45 bronze badges
8 Answers
Reset to default 5You're closer than you think - moving return null;
out of for (key in obj)
is the main thing; otherwise, you're giving up as soon as the first key in the object doesn't match. Only give up after searching all the keys.
function lookup(obj, k) {
for (var key in obj) {
var value = obj[key];
if (k == key) {
return [k, value];
}
if (typeof(value) === "object" && !Array.isArray(value)) {
var y = lookup(value, k);
if (y && y[0] == k) return y;
}
if (Array.isArray(value)) {
// for..in doesn't work the way you want on arrays in some browsers
//
for (var i = 0; i < value.length; ++i) {
var x = lookup(value[i], k);
if (x && x[0] == k) return x;
}
}
}
return null;
}
var json = {
"app": [{
"Garden": {
"Flowers": {
"Red flower": "Rose",
"White Flower": "Jasmine",
"Yellow Flower": "Marigold"
}
},
"Fruits": {
"Yellow fruit 1": "Mango",
"Green fruit 2": "Guava",
"White Flower 3": "groovy"
},
"Trees": {
"label": {
"Yellow fruit 2": [{
"type a": "Pumpkin",
"type b": "Banana"
}],
"White Flower 2": ["Bogan 1", "Bogan 2"]
}
}
}]
}
function type(object) {
var stringConstructor = "test".constructor;
var arrayConstructor = [].constructor;
var objectConstructor = {}.constructor;
if (object === null) {
return "null";
} else if (object === undefined) {
return "undefined";
} else if (object.constructor === stringConstructor) {
return "String";
} else if (object.constructor === arrayConstructor) {
return "Array";
} else if (object.constructor === objectConstructor) {
return "Object";
} else {
return "null";
}
}
console.log(lookup(json, 'type a'));
console.log( lookup(json, 'White Flower 2') );
p.s. There is no such thing as a "JSON object". If it's not a string, it's not JSON. You're searching through JavaScript objects.
function lookup(obj, k) {
if(typeof(obj) != 'object') {
return null;
}
var result = null;
if(obj.hasOwnProperty(k)) {
return obj[k];
} else {
for(var o in obj) {
result = lookup(obj[o], k);
if(result == null) continue;
else break;
}
}
return result;
}
var json = {
"app": [
{
"Garden": {
"Flowers": {
"Red flower": "Rose",
"White Flower": "Jasmine",
"Yellow Flower": "Marigold"
}
},
"Fruits": {
"Yellow fruit 1": "Mango",
"Green fruit 2": "Guava",
"White Flower 3": "groovy"
},
"Trees": {
"label": {
"Yellow fruit 2": [{"type a":"Pumpkin", "type b": "Banana"}],
"White Flower 2": ["Bogan 1", "Bogan 2"]
}
}
}
]
}
var rs = lookup(json,'type a');
console.log(rs);
You can use the below code -
var yourData = $.map(obj, function(el) { return el });
//Searching for the key in stored data
var result = $.grep(yourData, function (e) {
return e.key == your_search_key;
});
It will return you all matches and you can access like result[0]
Hope this will help you.
This is a recursive version, written in ES6 (but to convert to ES5 you just need to replace the arrow function with a normal one and replace the const
s with var
s).
// We could have just returned null if there was no match, but then it wouldn't work if your data contained null.
const notFound = {};
function lookup(obj, search) {
// Iterate over the object.
Object.keys(obj).forEach(key => {
// If we found the key we're looking for, return the matching value.
if (key === search) {
return obj[key];
// If we've got a nested object, recursively call lookup on it.
// If this object has the key we're looking for, return the matching value.
} else if (obj[key].constructor === {}.constructor) {
const result = lookup(obj[key], search);
if (result !== notFound) return result;
}
// Otherwise just go onto the next iteration.
});
// If iterating didn't return any matching keys, return notFound.
return notFound;
}
Not elegant but funny and performant!
function get(data, key) {
let str = JSON.stringify(data);
let r = new RegExp(`${key}":"([^"]{1,})"`);
let res = r.exec(str);
return res && res.pop() || null;
}
var data = {
"app": [
{
"Garden": {
"Flowers": {
"Red flower": "Rose",
"White Flower": "Jasmine",
"Yellow Flower": "Marigold"
}
},
"Fruits": {
"Yellow fruit 1": "Mango",
"Green fruit 2": "Guava",
"White Flower 3": "groovy"
},
"Trees": {
"label": {
"Yellow fruit 2": [
{
"type a": "Pumpkin",
"type b": "Banana"
}
],
"White Flower 2": [
"Bogan 1",
"Bogan 2"
]
}
}
}
]
};
console.log('result', get(data, "type a"));
EDIT: Question was asking for plain js or jquery, my answer uses a different library. However I'm still going to leave it as reference as it is a clean solution to the actual problem.
Original Answer
We use object-scan for all our data processing needs. Powerful once you wrap your head around how to use it and makes it easy to maintain the code. Here is how you'd answer your question
// const objectScan = require('object-scan');
const lookup = (data, term) => objectScan(['**'], {
abort: true,
rtn: 'value',
filterFn: ({ property }) => property === term
})(data);
const json = { app: [{ Garden: { Flowers: { 'Red flower': 'Rose', 'White Flower': 'Jasmine', 'Yellow Flower': 'Marigold' } }, Fruits: { 'Yellow fruit 1': 'Mango', 'Green fruit 2': 'Guava', 'White Flower 3': 'groovy' }, Trees: { label: { 'Yellow fruit 2': [{ 'type a': 'Pumpkin', 'type b': 'Banana' }], 'White Flower 2': ['Bogan 1', 'Bogan 2'] } } }] };
console.log(lookup(json, "type a"));
// => Pumpkin
console.log(lookup(json, "White Flower 2"));
// => [ 'Bogan 1', 'Bogan 2' ]
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>
Disclaimer: I'm the author of object-scan
// You can use this code to get the array of object key-value, with that key //and its value Where Data is a Json object and key is the Key u want to //search
function lookup(obj, k) { let values = [];
for (var key in obj) { var value = obj[key];
if (k == key)
{
values.push({key,value});
}
if (typeof value === "object" && !Array.isArray(value)) {
var y = lookup(value, k);
// values.push(y);
values= values.concat(y);
}
if (Array.isArray(value)) {
// for..in doesn't work the way you want on arrays in some browsers
//
for (var i = 0; i < value.length; ++i) {
var x = lookup(value[i], k);
values= values.concat(x);
}
}
}
return values;
}
console.log( lookup(Data, 'key') );
A little updated version that also looks in to array of objects and returns multiple occurences.
function lookup(obj, search, result = {}) {
if (!result[search]) {
result = {
[search]: []
};
}
Object.keys(obj).forEach(key => {
if (key === search) {
result[search].push(obj[key])
} else if (obj[key].constructor === {}.constructor) {
lookup(obj[key], search, result);
} else if (obj[key].constructor === [].constructor) {
for (const item of obj[key]) {
lookup(item, search, result);
}
}
});
return result;
}
const testData = {
group1: {
id: 'group1-id',
query: {
name: 'getTreees',
params: {
id: 'tree1'
}
},
fields: [
{
tree: 'oak',
query: {
name: 'getLeaves',
params: {
id: 'oak'
}
}
},
{
tree: 'chestnut',
query: {
name: 'getLeaves',
params: {
id: 'chestnut'
}
}
}
],
sometOtherData: 'boo'
}
}
console.log(lookup(testData, 'query'))