1

When I run this code/try to decode this

var data = JSON.parse({"forms":[{"url":"example.com/example","name":"example"}]})
document.getElementById("name").innerHTML=data.forms.name

the value returned undefined.

So what is the proper way to decode it using web javascript?

1
  • That is because data.forms contains an array, not an object. Commented Apr 20, 2018 at 14:28

2 Answers 2

2

Just use the object itself?

var data = {"forms": [{"url":"example.com/example","name":"example"}]};
document.getElementById("name").innerHTML=data.forms[0].name

JSON.parse() is taking string as a parameter. You don't need to parse it, because you already have the object itself.

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

Comments

0

In your example you are providing a JSON object, yuo don't have to parse it.

const data = JSON.parse('{"forms":[{"url":"example.com/example","name":"example"}]}')
document.getElementById("name").innerHTML=data.forms[0].name

And the 'form.data' field is an array. you should access it before reading any property.

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.