I have the following json object and trying to iterate with jQuery each but it only returns one iteration. What am I not doing right?
var data = "{
"SIEcat4": {
"3001": {
"test": "test",
"test": "test"
},
"3300": {
"test": "test",
"test": "test"
}
},
"SIEcat5": {},
"SIEcat6": {},
"SIEcat13": {
"3990": {
"test": "test",
"test": "test"
}
}
}"
$(JSON.parse(data)).each(function(key, value) {
console.log("test");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
In your question your JSON object is wrapped in a string, that's not right. If you have a string representation of an object you will have to parse that as a separate step. If you have a real JSON object then you can use
Object.keysto iterate over the keys of that object.Also, jQuery is not needed at all for this.
Here's how you can loop over the keys and data in an object
If your data is a string representation of JSON data you can do the exact same thing as above, but you will need to run
JSON.parseover it first to get the data out of the string.