Question posted in Json Our archive of expertly curated questions and answers provides insights and solutions to common problems related to this popular data interchange format. From parsing and manipulating JSON data to integrating it with various programming languages and web services, our archive has got you covered. Start exploring today and take your JSON skills to the next level
You can use the following approach if you are unsure of the JSON field name in advance.
public function main() {
json data = {"vals": ["a", "b", "c"]};
if data !is map<json> {
return;
}
foreach [string, json] [fieldName, value] in data.entries() {
if value is json[] {
io:println("Field Name : ", fieldName);
io:print("Array Values : ");
iterateJsonArray(value);
}
}
}
function iterateJsonArray(json[] arr) {
foreach json member in arr {
io:print(member, " ");
}
}
2
Answers
We need to narrow the type down to an iterable type after accessing the field as shown above.
You can use the following approach if you are unsure of the JSON field name in advance.