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

150
Views
How to get one value from JSON format using JS

I would like to get only one data type from a JSON file using JS.
the field that I want to get is "name".
the JSON format is:

{"countries":
 {"country":[
  {"id":"1","name":"Europe","active":"on","dir":"yes"}, 
  {"id":"2","name":"Africa","active":"on","dir":"yes"},
  {"id":"3","name":"North America","active":"on","dir":"yes"}, 
 ]}
}

the require result is:
Europe
Africa
North America

Thanks for the help

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

0

This has nothing to do with JSON. Your code represents a (javascript) Object literal/initializer.

From that Object you can map the name property of each entry of the nested array from countries.country.

const myObj = { "countries":
 {"country":[
  {"id":"1","name":"Europe","active":"on","dir":"yes"}, 
  {"id":"2","name":"Africa","active":"on","dir":"yes"},
  {"id":"3","name":"North America","active":"on","dir":"yes"}, 
 ]}
};

const countryNames = myObj.countries.country.map( c => c.name );

console.log(countryNames);

about 4 years ago · Juan Pablo Isaza Report

0

Accepted answer saves a step from mine - noted, thanks.

let obj = {"countries":
 {"country":[
  {"id":"1","name":"Europe","active":"on","dir":"yes"}, 
  {"id":"2","name":"Africa","active":"on","dir":"yes"},
  {"id":"3","name":"North America","active":"on","dir":"yes"}, 
 ]}
}
let objArray = obj.countries.country;
let names = objArray.map(item => item['name']);
console.log(names);

Returns an array of names ['Europe', 'Africa', 'North America']

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

about 4 years ago · Juan Pablo Isaza Report

0

let countryNames = data.countries.country.filter(item => item.name !== '')

OR

const countryNames = []
data.countries.country.forEach(item => {
     countryNames.push(item.name)
})

console.log(countryNames)
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!