0

I am doing a get request on JS and with the json reponse i want to get some values, for example product and color, but i don't know how i can go more deeper on the next levels of the objects of the Json.

the Json:

{    
"items": {        
    "1man": {
        "312favorites": {
            "155description": {
                "color": "red",
                "price": 2.76666
            }
          }
        }
    }

}

I just can go until the 1man level i don't know how to get the next levels, my code until now

console.log(response.data.items['1man']);

If i try for example console.log(response.data.items['1man'].312favorites i get error on VScode, i want go to until the color value...

Does someone can help me here?

2
  • 2
    response.data.items['1man']['312favorites']['155description'].color Commented Mar 18, 2022 at 23:53
  • 1
    If the key begins with a number, you need to use square bracket notation like @GabrielePetrioli shows ↑ Commented Mar 18, 2022 at 23:57

1 Answer 1

1

You have to do it this way:

response.data.items['1man']['312favorites']['155description'].color

The reason is that the key has some numbers, and it can't be accessed with Dot notation because it has to be without any numbers or special characters that's why you must use Bracket notation .

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you a lot guys, before i had tried response.data.items['1man'].['312favorites'], but with the explanation i know now why it doesn't worked, thanks again.
Sure, you are welcome

Your Answer

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.