1

I need to access the "State" value from the following array --

data = 
{ 
 Images:
 [ 
 {
 ProductCodes: [],
 BlockDeviceMappings: [Object],
 Tags: [],
 ImageId: 'ami-75301c',
 ImageLocation: '54696560/Test Image 3',
 State: 'available', 
 VirtualizationType: 'pavirtul',
 Hypervisor: 'xen' 
 }
 ],
 requestId: '2eb809d3-7f82-4142-b5d1-6af3' 
}

When I try data.Images["State"] or data.Images.State I get undefined.

Thanks

Can
8,60151 silver badges57 bronze badges
asked Jul 22, 2013 at 18:07
3
  • 1
    Good, that means everything is working properly. Your Array referenced by Images has no State property. If you indent your structure cleanly, things may become clear. Commented Jul 22, 2013 at 18:08
  • How do I access the State value? I need'available'back. Commented Jul 22, 2013 at 18:10
  • 1
    Well, Images is referencing an Array, and an Array is an ordered collection of data accessible via 0-based indices. Commented Jul 22, 2013 at 18:10

3 Answers 3

6

Images maps to an array which stores objects, so you have to specify the index of the item you want. Try data.images[0]["State"].

answered Jul 22, 2013 at 18:09
Sign up to request clarification or add additional context in comments.

1 Comment

You should probably check if there are any array elements prior to getting the state item inside that array. An array could be empty.
0

You can access like this:

data.Images[0].State

Or even:

data.Images[0]['State']
DINA TAKLIT
8,51410 gold badges85 silver badges92 bronze badges
answered May 7, 2019 at 13:37

Comments

-1

Access the state with data.image[0].state. Your method was wrong because inside the image, you need an index within the two square bracket, the image property is an array.

gre_gor
6,66112 gold badges105 silver badges106 bronze badges
answered Jun 21, 2017 at 17:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.