Reputation: 445
I have the following information captured from a form, and the data is in json format (i believe?)
var obj = {
"schema":{
"type":"object",
"title":"Event Info",
"required":[
"name",
"emergency_contact_name",
"emergency_contact_no",
],
"properties":{
"name":{
"type":"string",
"minLength":3,
"maxLength":10
},
"medical_conditions":{
"title":"Medical Conditions",
"type":"string",
"maxLength":120
},
"emergency_contact_name":{
"title":"Emergency Contact Name",
"type":"string",
"maxLength":120
},
"emergency_contact_no":{
"title":"Emergency Contact Number",
"type":"string",
"maxLength":120
}
}
}
}
So I would like to get the "required" fields only. I have tried obj['schema']['required'], and obj.schema.required, and obj['schema'].required, obj[0]['schema']['required'], obj[0].schema.required. None of these works. How is it possible to easily retrieve the attributes that i want?
Thanks.
Upvotes: 1
Views: 50
Reputation: 48367
As you mentioned in your comments, console.log(typeof obj)
prints string
and that means you need to convert your string to javascript object.
For this, you have to use JSON.parse
method.
obj = JSON.parse(obj);
let required = obj['schema']['required'];
Upvotes: 2