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

87
Views
Is it possible to search for a specific element in a json script with a variable

The following script is an example script. I need it to log the name Joe;

const names = `

    {
         "Joe": {
              "nat": "American",
              "hair": "brown"
         },
         "Peter": {
              "nat": "German",
              "hair": "blond"
         }
    }
`;

const load = JSON.parse(names);

var search = "Joe"

console.log(names.search);

The last line outputs "undefined" because it's searching for "search" instead of the value of the variable search. Before you ask: yes, I need it to be a variable (because the value of the variable would actually be the value of an input). Is there any way I can search the value of a variable in the JSON bit?

Thanks in advance

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Almost there (you still have to parse it first):

const names = `

    {
         "Joe": {
              "nat": "American",
              "hair": "brown"
         },
         "Peter": {
              "nat": "German",
              "hair": "blond"
         }
    }
`;

const load = JSON.parse(names);

var search = "Joe"

console.log(load[search]);
about 4 years ago · Juan Pablo Isaza Report

0

Try using names[search] instead

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!