0

hii i'm new to javascript i have an web request and its give response as JSON format, the task is that i need to parse the data into array

here is my sample reponse :

"Employee" : {
  "Employee_Names" : [
     {
        "BAR_RATING" : "0",
        "Name" : "anand",
        "Name" : "0",
        "PATTERN" : "Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "av",
        "Name_RATING" : "0",
        "PATTERN" : "FiLi",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "books",
        "Name_RATING" : "0",
        "PATTERN" : "Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "kanagalu",
        "Name_RATING" : "0",
        "PATTERN" : null,

     },
     {
        "BAR_RATING" : "0",
        "Name" : "specialty-av",
        "Name_RATING" : "0",
        "PATTERN" : "Fn-Ln",

     }
  ],
  "FOUND_Name" : [ null ],
  "OTHER_Name" : [
     {
        "BAR_RATING" : "0",
        "Name" : "kindle-cs-support",
        "Name_RATING" : "0",
        "PATTERN" : null,

     },
     {
        "BAR_RATING" : "0",
        "Name" : "noreply-ops-jobs",
        "Name_RATING" : "0",
        "PATTERN" : null,

     }
  ],
  "PERSONAL_Name" : [ null ],
  "PROJECTED_Name" : [
     {
        "BAR_RATING" : "0",
        "Name" : "anand.venkatesan",
        "Name_RATING" : "0",
        "PATTERN" : "Fn.Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "anandv",
        "Name_RATING" : "0",
        "PATTERN" : "FnLi",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "vanand",
        "Name_RATING" : "0",
        "PATTERN" : "LiFn",

     }
  ]
  },

what i need is i need from Employee_Names object i want all Name in one array similarly From OTHER_Name i want all Name to be stored in array

i dono how to parse the data with dynamic Array size

2
  • 1
    You should never do somethig like "PERSONAL_Name" : [ null ] do instead "PERSONAL_Name" : [] Commented Apr 6, 2017 at 7:18
  • show how should look the expected result Commented Apr 6, 2017 at 7:24

3 Answers 3

2

To get all values in the field "name" from the Employee.Employee_Names array

var resultArray = Employee.Employee_Names.map(function(a) {return a.Name;});

resultArray will then be ["anand", "av", "books", "kanagalu", "specialty-av"]

Sign up to request clarification or add additional context in comments.

2 Comments

@Krishna9960 as map being part of ES6 you can optimize it even more with fat arrow function, see my answer.
@loelsonk : Agreed.
2

If I understood you correctly, this should suit your needs.

We loop through "Employee_Names" objects.

// ES6 code    
var allNames = employee["Employee_Names"].map(bar => bar.Name);

// pure javascript
var allNames = [];
for (i = 0; i < employee["Employee_Names"].length; i++) {
  var element = employee["Employee_Names"][i];

  // We push only name
  allNames.push( element.Name );

  // We push full json object
   //allNames.push( element );
}

Now we assign new json key with our allNames array.

employee = Object.assign(employee, { "NEW_NAMES" : allNames });

Check working exmaple:

var employee = {
  "Employee_Names" : [
     {
        "BAR_RATING" : "0",
        "Name" : "anand",
        "Name_RATING" : "0",
        "PATTERN" : "Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "av",
        "Name_RATING" : "0",
        "PATTERN" : "FiLi",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "books",
        "Name_RATING" : "0",
        "PATTERN" : "Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "kanagalu",
        "Name_RATING" : "0",
        "PATTERN" : null,

     },
     {
        "BAR_RATING" : "0",
        "Name" : "specialty-av",
        "Name_RATING" : "0",
        "PATTERN" : "Fn-Ln",

     }
  ],
  "FOUND_Name" : [ ],
  "OTHER_Name" : [
     {
        "BAR_RATING" : "0",
        "Name" : "kindle-cs-support",
        "Name_RATING" : "0",
        "PATTERN" : null,

     },
     {
        "BAR_RATING" : "0",
        "Name" : "noreply-ops-jobs",
        "Name_RATING" : "0",
        "PATTERN" : null,

     }
  ],
  "PERSONAL_Name" : [  ],
  "PROJECTED_Name" : [
     {
        "BAR_RATING" : "0",
        "Name" : "anand.venkatesan",
        "Name_RATING" : "0",
        "PATTERN" : "Fn.Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "anandv",
        "Name_RATING" : "0",
        "PATTERN" : "FnLi",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "vanand",
        "Name_RATING" : "0",
        "PATTERN" : "LiFn",

     }
  ]
  };


// ES6
//var allNames = employee["Employee_Names"].map(bar => bar.Name);

// pure javascript
var allNames = [];
for (i = 0; i < employee["Employee_Names"].length; i++) {
  var element = employee["Employee_Names"][i];

  // We push only name
  allNames.push( element.Name );
  
  // We push full json object
   //allNames.push( element );
}

// Now we assign new json key with our names
employee = Object.assign(employee, { "NEW_NAMES" : allNames });

// Our new employee json
console.log(employee);

Comments

1

Use Array map() method to get all the particular property values from an array of object into a single array.

To fetch all the names into an single array from Employee_Names :

  var empNames = employee.Employee_Names.map(function(item) {
    return item.Name;
  });

To fetch all the names into an single array from OTHER_Name :

  var otherNames = employee.OTHER_Name.map(function(item) {
    return item.Name;
  });

Working Demo

var employee = {
  "Employee_Names" : [
     {
        "BAR_RATING" : "0",
        "Name" : "anand",
        "Name_RATING" : "0",
        "PATTERN" : "Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "av",
        "Name_RATING" : "0",
        "PATTERN" : "FiLi",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "books",
        "Name_RATING" : "0",
        "PATTERN" : "Ln",

     },
     {
        "BAR_RATING" : "0",
        "Name" : "kanagalu",
        "Name_RATING" : "0",
        "PATTERN" : null,

     },
     {
        "BAR_RATING" : "0",
        "Name" : "specialty-av",
        "Name_RATING" : "0",
        "PATTERN" : "Fn-Ln",

     }
  ],
  "FOUND_Name" : [ null ],
  "OTHER_Name" : [
     {
        "BAR_RATING" : "0",
        "Name" : "kindle-cs-support",
        "Name_RATING" : "0",
        "PATTERN" : null,

     },
     {
        "BAR_RATING" : "0",
        "Name" : "noreply-ops-jobs",
        "Name_RATING" : "0",
        "PATTERN" : null,

     }
  ]
  };
  
  var empNames = employee.Employee_Names.map(function(item) {
    return item.Name;
  });
  
  var otherNames = employee.OTHER_Name.map(function(item) {
    return item.Name;
  });  
  
  console.log("Employee Names", empNames);
  console.log("Other Names", otherNames);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.