Muhammad Waqas
Muhammad Waqas

Reputation: 1

how to convert a string to json object using jquery

str = {'8': {'name': 'breeze bedsheet ', 'price': 1150, 'qty': 1, 'strid': '8', 'image': 'images/download_1.jpg'}, '10': {'name': 'neat and clean bedsheet ', 'price': 1150, 'qty': 2, 'strid': '10', 'image': 'images/images_2.jpg'}}

obj = JSON.parse(str)

I want to convert this string to JSON object but when i use JSON.parse method it gives me an error like

uncaught syntaxError: Unexpected token ' in JSON at position 1

Upvotes: 0

Views: 45

Answers (1)

AoooR
AoooR

Reputation: 441

It is because you use ' instead of "

str = "{'8': {'name': 'breeze bedsheet ', 'price': 1150, 'qty': 1, 'strid': '8', 'image': 'images/download_1.jpg'}, '10': {'name': 'neat and clean bedsheet ', 'price': 1150, 'qty': 2, 'strid': '10', 'image': 'images/images_2.jpg'}}";

obj = JSON.parse(str.replaceAll("'", '"'));

console.log(obj);

Upvotes: 2

Related Questions