#Get JSON .property pointer from a string input?

10 messages · Page 1 of 1 (latest)

grim eagle

I have more of a general javascript question:
If i want to have a method get a .property from a json file, but i want which property to get to be inputted in the form of a string, how can I turn that string into the .property?

Here's an example, since i know that makes no sense:
If jsonObject is a valid JSON object retrieved from reading a file, and the json looks like this:{ keyOne: "seventeen", keyTwo: "fifty" }And I also have this method here:async function returnValue(input) { const getKey = input return jsonObject.getKey }Now let's say I want to get jsonObject.keyOne, which is "seventeen", and I do returnValue(keyOne) somewhere in my code. Obviously this wouldn't work, but i don't know what I need to do to make it work. How would I change the returnValue function so it does what I'm trying to do?

I know it's something relatively simple, but I can't for the life of me remember how to do it dogeHaHa

noble forge

input[“keyOne”]?

const json = require(json path)

json[“keyOne”] = “seventeen”

grim eagle

so by that logic, would returnValue(keyOne) return seventeen if i changed returnValue to this?async function returnValue(input) { return jsonObject[input] }

noble forge

if jsonObject is what I said with the const json = require(…), yes I believe so

Object[.] gets something out of an object

{
“hi”: “bye”
}
Object[“hi”] would return “bye” iirc

grim eagle

I see, thank you!
I'll keep this thread open for a bit, just in case it doesn't work dogeHaHa