I refer to many sites about recursion of json as below
JavaScript recursion does not work properly
jQuery recursive iteration over objects
but most of them either knows the key or iterate using know keys. Also $.each work wells if key & value are both string such as {"firstname" : "lastname"} & then function (k,v) can give key & value as k & v respectively.
But in case if json object is complex & we do not whats coming that is key can be any for example in JSON1 & JSON2 are different in keys one, three, two in JSON1 is replaced by five, six & ten in JSON2 response.
JSON1
{
"one": true,
"three": [
"red",
"yellow",
[
"blue",
"azure",
"cobalt",
"teal"
],
"orange"
],
"two": 19.5
}
JSON2
{
"five": true,
"six": [
"red",
"yellow",
[
"blue",
"azure",
"cobalt",
"teal"
],
"orange"
],
"ten": 19.5
}
If some how we can know the key name & then compare it would be ok. easier to compare the keys, for this tagName should be work as suggested in URL below
Can jQuery provide the tag name?
But for normal json object attribute tagname does not seem to work. Inspecting in firefox attributes such as text(), attr(), does not get me tagName. What I want is to loop through the json object (in practice i am using xml2json plugin to convert xml into above json format) & compare key if exists and do action. I know in XML you can do the same by using nodeName. How should I do the same using JSON object.