Skip to content
Advertisement

How to parse this json with multiple objects

devs I am stuck in parsing this kind of JSON I don’t understand how to get the value of status and message any help will be very appreciable. I get the value of error but when I want to access the value of status and message it throws an error JSON Format :

JavaScript

My code for parsing json :

JavaScript

Advertisement

Answer

JavaScript

The above command will give you the response as JSON.

To get Status and message, you need to extract error as a separate JSONObject and then parse status and message

JavaScript

Remember to parse and retrieve using the hierarchy.. so if the status & message are inside “error”, then extract error as a JSONObject and then retrieve the child keys. And as a good practice check if the key exists or not:-

JavaScript

Adding a working sample :-

JavaScript

Output :-

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement