0
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

1
  • There is no reason to parse anything - you already have an object. str is not a string at all. Commented Jun 22, 2022 at 8:10

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sir, You have solve my problem which i have face for two days thank a lot
@MuhammadWaqas No problem. Could you turn my awnser into solution please ?

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.