Avatar of pravetz
pravetz

asked on 

JSON Array Loop in Javascript (newbie question)

I have the following JSON object:

var json = {"Product":[{"Product_Title":"Beach hat"},{"Product_Title":"Blue Jeans"}]};

I am using the following loop to get records:

for (var x in json.Product) {
      alert(json.Product[x].Product_Title);
}

This code is working fine while there is more than one record. When my JSON looks like this (single record):

var json = {"Product":{"Product_Title":"Blue Jeans"}};

I can no longer access the record from the above loop. I thought I should be able to access the single record like this "json.Product[0].Product_Title" however it turns out I should access it like this "json.Product.Product_Title" which means that I have to write more code to accommodate single records. I believe there should be a better solution please help.
JavaScriptJScript

Avatar of undefined
Last Comment
hielo
Avatar of hielo
hielo
Flag of Wallis and Futuna image

>>I thought I should be able to access the single record like this "json.Product[0].Product_Title"
That zero in Product[0] works because on your first sample code, "Product" IS an array of objects. Notice that it has:
"Product":[....]

the other one is NOT an array. It is a direct object assignment.
var json = {"Product":{"Product_Title":"Blue Jeans"}};
for( i in json )
{
alert( i );
	for( j in json[i] )
	{
		alert( j + " = " + json[i][j] );
	}
}

Open in new window

Avatar of pravetz
pravetz

ASKER

I guess you are right. So there is no elegant solution to this?
If the single record was formatted like this it would work:

var json = {"Product":[{"Product_Title":"Blue Jeans"}]};

Unfortunately I am getting the JSON from a converter (Json.NET) and I have no control over the formatting.

My current solution is something like this (could it be better?):
if (json.Product.length == undefined){
alert(json.Product.Product_Title);
}
 
else {
 
for (var x in json.Product) {
      alert(json.Product[x].Product_Title);
}
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of pravetz
pravetz

ASKER

Yeah thats better than mine solution!
What is this part about?

var t = new Array();

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

>>What is this part about?
Sorry. Not necessary. I was initially declarring an array variable and then pushing data into the array, but since this is a one element array then you can do it directly via new Array() like I'm doing here:
json.Product = new Array(json.Product );
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo