0

I am trying to parse a json string inside of a JSON object but continue getting "Unexpected Token" Or "unexpected end of input" errors. Any help would be greatly appreciated!

The syntax looks like so:

{
  full_name: 'John Doe',
  company_name: 'TEST',
  sample: 't-e-s-t',
  populated: '{"type":"Select","manualTitle":"Manual Title","manualBody":"Email Message will go here","templateTitle":"Title Here","templateBody":"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old."}',
  messageBody: 'New body test'
}
4
  • 1
    What does the JS you're using to parse the JSON look like? Commented Jun 7, 2021 at 14:06
  • 2
    That's a JS object, not a JSON object. (Thepopulated property is JSON) Commented Jun 7, 2021 at 14:08
  • 1
    JSON.parse(obj.populated) Commented Jun 7, 2021 at 14:08
  • @Kinglish OP calls their object literal a "JSON object" (which is a term without any right to exist). But yes, it works fine: jsfiddle.net/2mqxr0kn Commented Jun 7, 2021 at 14:10

1 Answer 1

4

That's a JavaScript object, not JSON. (JSON is a string and has a very specific syntax.) So use JSON.parse to parse the value of the populated property of the object, and then you can extract its keys and values.

const obj = {
  full_name: 'John Doe',
  company_name: 'TEST',
  sample: 't-e-s-t',
  populated: '{"type":"Select","manualTitle":"Manual Title","manualBody":"Email Message will go here","templateTitle":"Title Here","templateBody":"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old."}',
  messageBody: 'New body test'
};

const populated = JSON.parse(obj.populated);
console.log(populated.manualBody);

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

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.