5

I need to convert my JSON response to an object, how can I achieve this?

My JSON response:

[  
   {  
      "id":296,
      "nama":"Appetizer"
   },
   {  
      "id":295,
      "nama":"Bahan"
   }
]
0

2 Answers 2

17

Provided your response is a valid JSON, just do this

var obj = JSON.parse(response);
Sign up to request clarification or add additional context in comments.

Comments

4

You need to use JSON.parse in a try catch to catch errors if JSON is not valid.

let str = '[{"id":296,"nama":"Appetizer"},{"id":295,"nama":"Bahan"}]';

try {
  let obj = JSON.parse(str);
} catch (ex) {
  console.error(ex);
}

To convert back your object to string, use Stringify

JSON.stringify(obj)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.