How to parse only specific attributes from a JSON file to an array

Enguias

I have this JSON array:

{
  "data": [
    {
      "id": 6,
      "pnome": "dasda",
      "unome": "dad",
      "avatar": 1,
      "email": "d",
      "pass": "password",
      "ponto": 0
    },
    {
      "id": 3,
      "pnome": "Enguias",
      "unome": "Enguias outra vez",
      "avatar": 10,
      "email": "[email protected]",
      "pass": "enguias",
      "ponto": 0
    },
    {
      "id": 2,
      "pnome": "André",
      "unome": "Marques",
      "avatar": 1,
      "email": "[email protected]",
      "pass": "yet1",
      "ponto": 0
    }
  ]
}

And i'm putting it into an array with axios, like this:

    axios.get(my_url_api)
    .then((res)=>{
        const txt = JSON.stringify(res.data.data);
        const users = JSON.parse(txt)
    })

Which makes the users array have all the info in the JSON file. How can I make it so that I only pass specific attributes like email and pass? It would look like this:

{
  "data": [
    {
      "email": "d",
      "pass": "password"
    },
    {
      "email": "[email protected]",
      "pass": "enguias"
    },
    {
      "email": "[email protected]",
      "pass": "yet1"
    }
  ]
}
Robin Mollah

You can use Array.map() function to remove the unnecessary fields like following example:

let res = {
"data": [
{
"id": 6,
"pnome": "dasda",
"unome": "dad",
"avatar": 1,
"email": "d",
"pass": "password",
"ponto": 0
},
{
"id": 3,
"pnome": "Enguias",
"unome": "Enguias outra vez",
"avatar": 10,
"email": "[email protected]",
"pass": "enguias",
"ponto": 0
},
{
"id": 2,
"pnome": "André",
"unome": "Marques",
"avatar": 1,
"email": "[email protected]",
"pass": "yet1",
"ponto": 0
}
]
}

res.data = res.data.map(item => {
  return {
  email: item.email,
  pass: item.pass
  };
});

console.log(res);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to parse only specific objects without deserializing the whole JSON file?

How to parse for specific unique values from a JSON lines file with Python and store into an array

How to get only some attributes from json file?

How to parse specific Json Array

How to parse specific data from JSON file to a html dropdown menu

Python parse nested JSON file and take out specific attributes

Parse JSON file and extract only array of numbers

How to parse one array at a time from External JSON file in Intervals

How to gather only specific data from remote JSON file

How to parse this json from file

How to parse android attributes from JSON converted from XML in Javascript?

How to parse JSON objects that depend on array contents for their attributes with JQ?

Parse an integer array from a JSON file

Fetching only a specific data from Json file

Python: Extract only some attributes from a csv file to a numpy array

Parse only specific columns from csv file using token

Display only specific parts of an array that has been retrieved from a JSON file

How to get attributes from specific objects in an array using jQuery?

How to Remove a specific items attributes from nested array of object in javascript

How to convert json to php array and only pull specific value from json

How to parse json array from mysql?

How to parse JSON from URL with unnamed array

how to parse data from json Array

How to parse array values from Json in Jmeter

How to parse data from file Json or Excel

How to parse JSON file from a column in BigQuery

How to Parse a JAVA ArrayList from a JSON file

How to parse json from file in sdcard

Get only specific attributes with from Laravel Collection