Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a particular value after JSON.stringify()

Tags:

javascript

This is the data displaying in console.log.

{"data":
    [
        {
            "CloserName":null,
            "agent_id":"10807",
            "AgentName":"TEST",
            "SurveyDate":"02/02/2018 02:18:46 AM",
            "SurveyName":"Ruth  ",
            "state":"West Bengal",
            "phone":"9836969715",
            "status":"Approved",
            "verification_progress":"Pending",
            "survey_id":"1",
            "rejection_remarks":"aa",
            "tl_remarks":"Pending"
        }
    ],
    "count":1
}

Can anyone help me display a single value (i.e survey_id)? I just want to fetch that survey_id

like image 373
Roy.N Avatar asked Sep 07 '25 14:09

Roy.N


2 Answers

Here's an example:

var json = {  
   "data":[  
      {  
         "CloserName":null,
         "agent_id":"10807",
         "AgentName":"TEST",
         "SurveyDate":"02/02/2018 02:18:46 AM",
         "SurveyName":"Ruth ",
         "state":"West Bengal",
         "phone":"9836969715",
         "status":"Approved",
         "verification_progress":"Pending",
         "survey_id":"1",
         "rejection_remarks":"aa",
         "tl_remarks":"Pending"
      }
   ],
   "count":1
}

// get first id
var id = json.data[0].survey_id
console.log(id)

// get all ids
var ids = json.data.map(x => x.survey_id)
console.log(ids)

If the JSON is stringified, call JSON.parse(jsonStr) first.

like image 155
Miguel Mota Avatar answered Sep 10 '25 05:09

Miguel Mota


You have to parse the JSON-String into an object. After that you can access the data with default object-identifiers.

const object = JSON.parse('{"data":[{"CloserName":null,"agent_id":"10807","AgentName":"TEST","SurveyDate":"02/02/2018 02:18:46 AM","SurveyName":"Ruth ","state":"West Bengal","phone":"9836969715","status":"Approved","verification_progress":"Pending","survey_id":"1","rejection_remarks":"aa","tl_remarks":"Pending"}],"count":1}');
console.log(object.data[0].survey_id)
like image 44
Phillip K Avatar answered Sep 10 '25 06:09

Phillip K