Home > Back-end >  how to read a property from a Json?
how to read a property from a Json?

Time:01-06

How can I read query.students.queryType?

query = {
  "students": [
    {
      "searchField": "lastname",
      "queryType": "exact",
      "query": {
        "text": "jack"
      }
    }
  ]
}

CodePudding user response:

Probably the problem is because of, query type is string (stringified json), and you are trying to get students property of a string type. So it is undefined. First convert the stringified json to a json object using:

const jsonObj = JSON.parse(query);

Then get the value of students members using forEach:

jsonObj.students.forEach(student => {
   console.log(student.queryType) 
   // you can get the value of queryType for each student 
});

CodePudding user response:

Access it like this:

query.students[0].queryType
  • Related