Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

330
Views
Trying to use a variable in a json file path - javascript

I'm trying to use a variable to access a field in my json file, how do I do something like this? js:

let file = require('./file.json')
let someVar = 'firstField'
console.log(file.firstObject.{someVar})

json:

{
  "firstObject": {
     "firstField":"firstValue",
     "secondField":"secondValue",
     "thirdField":"thirdValue",
  },
  "otherObject": {
     "otherField":"otherValue"
  }
}

desired outcome :

'firstValue'
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

object[str] is what you are looking for. Your code should look like this:

let file = require('./file.json')
let someVar = 'firstField'
console.log(file.firstObject[someVar])
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!