In javascript we can use json parser to parse json values. JSON.parse() take string as an argument and return an object.
How to Parse Json in Javascript
Let’s create one json string and stored in a variable.
1 |
var empData = '{"name": "jhon","age": 30,"designation": "manager"}'; |
To parse json data, we are using JSON.parse().It take as a string and return object.
1 |
var empValue = JSON.parse(empData); |
If you want to check what empValue variable contains, you can print
1 2 3 |
alert(typeof empValue); //It prints an object. |
If you want to check whether it’s an valid json or not
1 2 3 4 5 6 7 8 |
if(typeof empValue == 'object'){ // It's a valid json } else { // Invalid json } |
Print object values, here i am using for/in loop.
1 2 3 |
for(var key in empValue){ alert(empValue[key]); } |
If you want to access individual property of an object , you can do it by
1 2 |
console.log(empValue.name); console.log(empValue.age); |