Looping Through a Json Object Containing More Objects and Arrays

looping through Json object with multiple arrays Python3

Loop over everything instead of indexing on the first item.

for region in azure_json["regions"]:
for line in region["lines"]:
for word in line["words"]:
print(word["text"])

Output will be something like:

KINGDOM
OF
SAUDI
ARABIA
MINISTRY
OF
INTERIOR
االل
سياقة
رخصة
DRIVING
LICENSE

If you want to write to the file instead of printing to standard output, just use the write method -- your example code for some reason opens an output file but then does not write to it. Note that you will have to supply the newline ("\n") explicitly:

with open('data3.txt', 'w', encoding='utf-8') as f:
for region in azure_json["regions"]:
for line in region["lines"]:
for word in line["words"]:
f.write(word["text"] + "\n")

How to iterate over json Array having multiple json objects with different header

Based on the classes and methods you're using, I'm assuming you use org.primefaces.json classes. But even if you're using a different API, the logic will be basically the same.

First of all, look at your JSON structure:

[
{
"mutualFund": {
"fundCode": "XYZ",
"fundName": "Funds-GlobalIncomeFund(G)-SGD","isin":"LU0882574725","sedol":"1234"
}
},
{
"brokers": {
"fundID": "abcd",
"fundName": "Funds-FocusFundA-USD","isin":"LU0197229882","sedol":"6543"
}
}
]

It's an array with 2 elements. The first element is an object with just one key (mutualFund) and its value (another object with fundCode and fundName keys). Note that the object has a mutualFund key, and you're trying to get it as if the object itself was a mutualFund. That's what causes the error.

So, to get all mutualFund objects, you need to check every element in the array, and for each element you must check if it has a mutualFund key. Then your code will be like this:

for (int i = 0; i < jsonArray.length(); i++) {
// get object i
JSONObject jsonObject = jsonArray.getJSONObject(i);
// check if object has mutualFund key
if (jsonObject.has("mutualFund")) {
// get mutualFund object and do something with it
JSONObject mutualFund = jsonObject.getJSONObject("mutualFund");
// do something with mutualFund object (you can get values for fundCode and fundName keys, etc)
}
}

Note: if you're using a different JSON API, the methods names might differ (instead of has, some uses containsKey, or get(key) != null, but the logic to find mutualFund objects will be the same).

How to loop through multiple arrays within objects in a JSON API using JavaScript

Since data.groups is an array, you first have to iterate through them before accessing the equipments.

You can also iterate through them using a forEach, so that's easy!

data.groups.forEach(function(group) {
group.equipments.forEach(product) {
output += `
<div class="card">
<img src=${product.photos.text} alt=${product.model} />
<h3>${product.year} ${product.manufacturer} ${product.model}</h3>
<p>${product.hours} hours</p>
<a href='https://used.battlefieldequipment.ca/en/${product["group-code"]}/${product.equipments["serial-number"]}' class="btn btn-primary">View Details</a>
</div>`;
}
})

Looping through objects in JSON array

loop through json with multiple objects

try with

for (var i=0; i<json.length; i++) {
alert("JSON Data: " + json[i].pk_records_id);
// you need to write each key name here
}
DEMO

How to loop through json object, which has been converted to associative array, to get prop value

$element will refer to the values inside already.

forEach($assArr["features"] as $element){
if ($element["properties"]["iso_a2"] == "BS"){
echo_r($element);
}
}

If you need the 0, you can do the following

forEach($assArr["features"] as $key => $element){
if ($element["properties"]["iso_a2"] == "BS"){
echo_r($key); // will be 0
echo_r($element);
}
}

How to Loop through JSON Objects having Objects and Arrays Inside

Since holders object is an array, you can loop over it like below, and make use of the address like constructing the URL as per your logic inside the loop. Here's the example of storing the addresses in an array: