Nirodha Wickramarathna
Nirodha Wickramarathna

Reputation: 291

How to get particular data values from JSON Node.js?

This is my JSON file output:

let employees = [{
  "id":1,
  "name":"Lann",
  "username":"brot",
  "email":"[email protected]",
  "address": {
    "city":"Gweh",
    "zipcode":"92998-3874",
    "geo": {
      "lat":"45",
      "lng":"77"
    }
  }
}]

How I get id, name and email from that like below:

{
  "id":1,
  "name":"Lann",
  "email":"[email protected]"
}

Upvotes: 1

Views: 1966

Answers (4)

Santosh Gusain
Santosh Gusain

Reputation: 811

You can simply do this by using array destructuring.

let employees = [{
          "id":1,
          "name":"Lann",
          "username":"brot",
          "email":"[email protected]",
          "address":{
             "city":"Gweh",
             "zipcode":"92998-3874",
             "geo":{
                "lat":"45",
                "lng":"77"
             }
          }}];

// Destructuring array
const [employee] = employees;

** Now from here employee is an object and you can access its property normally as you do with other objects. For getting id, name, username:**

employee.id;
employee.name;
employee.username;

Upvotes: 2

Philippe
Philippe

Reputation: 1307

You cans also loop through your input if it contains multiple items and get an array of shrink items :

let employees = [{
  "id": 1,
  "name": "Lann",
  "username": "brot",
  "email": "[email protected]",
  "address": {
    "city": "Gweh",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "45",
      "lng": "77"
    }
  }
}]

let shrink = [];

for (let employee of employees) {
  shrink.push({
    id: employee.id,
    name: employee.name,
    email: employee.email
  });
}

console.log(shrink);

Upvotes: 1

Gabriel
Gabriel

Reputation: 690

If your array has only one element you can just access the info, no need to build another array like this: employees[0].id , employees[0].name, employees[0].email or you can just extract an object using Object Destructuring

let employees = [{
  "id": 1,
  "name": "Lann",
  "username": "brot",
  "email": "[email protected]",
  "address": {
    "city": "Gweh",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "45",
      "lng": "77"
    }
  }
}];
const picked = (({ id, name, email }) => ({  id, name, email }))(employees[0]);
console.log(picked);

but if your array has more employees, i think what you need to do is search by id or name and get back just an object with minimal info, you can do that like this

let employees = [{
  "id": 1,
  "name": "Lann",
  "username": "brot",
  "email": "[email protected]",
  "address": {
    "city": "Gweh",
    "zipcode": "92998-3874",
    "geo": {
      "lat": "45",
      "lng": "77"
    }
  }
}];
let employee = employees.find(o => o.name === 'Lann');
let picked = (({ id, name,email }) => ({ id, name,email }))(employee);
console.log(picked);

Upvotes: 3

Jai248
Jai248

Reputation: 1649

You can archive using map.

let employees = [{
          "id":1,
          "name":"Lann",
          "username":"brot",
          "email":"[email protected]",
          "address":{
             "city":"Gweh",
             "zipcode":"92998-3874",
             "geo":{
                "lat":"45",
                "lng":"77"
             }
          }
          }]
const data = employees.map(o => ({ id: o.id, name: o.name, email:o.email }));
console.log(data[0]);

Upvotes: 2

Related Questions