I have a page that sends a request to a RESTful web service (via php and curl). That page receives a JSON object as a response. I am trying to fill some form fields using the data that was returned. The object javascript that gets echoed onto my page looks like:
var obj = {
"NPI": [
{
"NPI": "123456789",
"EntityType": "Individual",
"IsSoleProprietor": "N",
"LastName": "Smith",
"FirstName": "John",
"MiddleName": "D",
"NameSuffix": "JR.",
"Credential": "MD",
"FirstLineMailingAddress": "PO BOX 123",
"MailingAddressCityName": "SCOTTSDALE",
"MailingAddressStateName": "AZ",
"MailingAddressPostalCode": "85255-0162",
"MailingAddressCountryCode": "US",
"MailingAddressTelephoneNumber": "888-123-4567",
"MailingAddressFaxNumber": "888-123-4567",
"FirstLinePracticeLocationAddress": "123 DR",
"SecondLinePracticeLocationAddress": "#278",
"PracticeLocationAddressCityName": "SCOTTSDALE",
"PracticeLocationAddressStateName": "AZ",
"PracticeLocationAddressPostalCode": "85266-2273",
"PracticeLocationAddressCountryCode": "US",
"PracticeLocationAddressTelephoneNumber": "888-123-4567",
"PracticeLocationAddressFaxNumber": "888-123-4567",
"EnumerationDate": "09/20/2006",
"LastUpdateDate": "02/07/2011",
"GenderCode": "M",
"Gender": "Male",
}
]
};
alert(obj.NPI.NPI);
What is the syntax to access these object properties. The alert statement below doesn’t work (it alerts “Undefined”).
Thanks for the help
try
obj.NPI[0].NPIthis should workthe propblem is that the first NPI is an array not an object so because of the presence of the [] so you should select which element in the array