How To Get Variable Property of JSON Object?

Hi all :wave:,

I am trying to fetch something (to be precise, a user’s profile) using the Glitch API. I’m getting the JSON object as the expected result and everything is fine, but I have one problem: the name of the object is based on the query.

Here’s what I mean:

A GET request to Glitch’s API for my profile (@khalby786), the object starts with

"khalby786": {
....
}

What happens is that when I try to get a certain property from the object, I have to get it as data.khalby786.name but the user I want is defined in a variable. What I’m asking is that can I refer to the property like this

car user = "jenn" // hi jenn :slight_smile:

// GET request
...
console.log(data.<USER>.name);

If so, how?

Wishing you all a good day!

You can get parts of a JSON object the same way as Array index notation, but it’s a little different.

var user = "jen"

var name = data[user].name

Hope this helps! :wave:

Oh, yeah, that worked! Thanks, @charliea21!