0

how do I use a nodejs var inside a json statement, I dont realy have the required vocabulary to explain but here is my simplifyed code:

test.json:

{
"test1":["test1.1", "test1.2"],
"test2":["test2.1", "test2.2"]
}

test.js:

const json = require("./test.json")
function myFunction(TYPE){
return(json.TYPE[0])
}
console.log(myFunction("test1"))

as I use the "type" var it tries to uses it as an json statement or object but obviously there is no "type" there only is "test1" and "test2" but it interprets as "type" instead

phi zim
  • 3
  • 2
  • Does this answer your question? [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Heretic Monkey Feb 07 '22 at 14:32
  • Also your variable `json` is not JSON, and there is no such thing as a "JSON object"; it is an object. The `require` statement loads the file and parses it to an object for you. – Heretic Monkey Feb 07 '22 at 14:36

2 Answers2

0

Brackets to access the variable key should work

function myFunction(TYPE){
return(json[TYPE][0])
}
0

In JavaScript, json objects are pretty much same as plain JS objects. You need to use string as an index to access properties:

// This could be your json file, its almost the same
// Just require it like you did instead of using const JSON like i am
const json = {
"test1":["test1.1", "test1.2"],
"test2":["test2.1", "test2.2"]
}

function myFunction(TYPE){
// Access the json by using keyword directly as index string
  return(json[TYPE])
  // because TYPE is a string, TYPE[0] would mean first letter of TYPE string
}

function myFunctionDeeper(TYPE){
  // To access a field and array in that field
  return(json[TYPE][0])
}

console.log(myFunction("test1"))
console.log(myFunctionDeeper("test1"))

// example for what happens when accessing string by index
console.log("test1"[0])

Read more about objects here

user3133
  • 592
  • 4
  • 12
  • thank u very much, I never thought about using 2 vars, I allways tried like json[type][0] or json[type[0]] but simply writing 2 vars like type = json[type] and type[0] works This took me so long – phi zim Feb 07 '22 at 14:31
  • There is no such thing as a "json object". Require on a JSON file parses the JSON to a JavaScript object. Using the correct terminology helps. – Heretic Monkey Feb 07 '22 at 14:37
  • @HereticMonkey I used json object to refer to the origin of the object, which is json file. Sorry if this is too confusing. – user3133 Feb 07 '22 at 14:40
  • @HereticMonkey did you just change the correct answer due to terminology you do not understand? – user3133 Feb 07 '22 at 14:42
  • @user3133 Please refrain from accusing people when *you* don't understand how the site works. I (as a user just like you) can not change the *accepted* answer. Only the OP (asker) can do that. Also, I understand the terminology, and my comment was correcting your incorrect use of the terminology. – Heretic Monkey Feb 07 '22 at 14:45
  • @HereticMonkey you should probably post an edit instead. – user3133 Feb 07 '22 at 14:46
  • @user3133 I've been accused of "vandalizing" people's answers when I've done that. The easiest route is when people admit to making a mistake and edit their own posts... – Heretic Monkey Feb 07 '22 at 14:49
  • @user3133 I changed the correct answer to the simple one, even when your option is also working and correct, the other solution is easier to understand for newcomers like me. – phi zim Feb 07 '22 at 15:05