1

I have a simple set of HTML and JS files that both spectacularly fail, and I can't figure out why:

<html>
 <head>
  <script type="text/javascript" src="data.json"></script>
 </head>
 <body>
 </body>
</html>
var data = '{"name" : "Phil Powell"}';
alert(data + ' ' + data.name + ' ' + data.type);

But every single time I try to open this up, I get the exact same error every single time:

{"name" : "Phil Powell"} undefined undefined

What am I doing wrong? All I want to do is parse an external JSON file, and I can't seem to do it.

Please help.

Thanks

5
  • undefined here is not an error, it's a value. Commented Jun 24, 2020 at 4:10
  • That makes no sense Commented Jun 24, 2020 at 4:48
  • No? "Error" is a term used when an unhandled exception occurs (execution breaks). Reading an unset property of an object is an exception which is handled automatically, we can say that all unset object properties have undefined as their value. Commented Jun 24, 2020 at 4:52
  • Teemu: You are not making sense. I cannot understand why I am producing an error when I have a simple .json file and can't even read it via Javascript via a simple file Commented Jun 24, 2020 at 5:01
  • 1
    You can't include a .json file using script tag. You need AJAX to read .json files. JSON is a textual data exchange format, it's not JavaScript. Commented Jun 24, 2020 at 5:04

6 Answers 6

2
var data = '{"name" : "Phil Powell"}';

remove the enclosing apostrophe lie shown below

var data = {"name" : "Phil Powell"};
Sign up to request clarification or add additional context in comments.

1 Comment

That also produced undefined
1

You data is a string. You need to first convert data to object to access by key.

var data = '{"name" : "Phil Powell","type":"something"}';
var obj = JSON.parse(data);

console.log(data + ' ' + obj.name + ' ' + obj.type);

1 Comment

Converting data to object also fails. It can't read it as a string nor object
1

Remove object quotes like :

var data = {"name" : "Phil Powell", "type" : "ABC"};
alert(data.name + ' ' + data.type);

1 Comment

When I do that, it produces undefined
1

I hope this helps

var data = {"name" : "Phil Powell", "type" : "Mars Volta"};
console.log(data['name'] + ' ' + data['type']);

var data = '{"name" : "Phil Powell", "type" : "Mars Volta"}';
var obj = JSON.parse(data);
console.log(obj.name + ' ' + obj.type);

1 Comment

That also produced an error: "Unterminated String Constant"
1

In two ways you can make it work,

1. Removing single quotes from the string

var data = '{"name" : "Phil Powell","type":"something"}';

instead of this use below

var data = {"name" : "Phil Powell","type":"something"};

console.log(data.name, data.type);

2. Convert your string to JSON object using JSON.parse()

`var data = '{"name" : "Phil Powell","type":"something"}';`

`var obj = JSON.parse(data);`

`console.log(data.name, data.type);`

Comments

1

did you try src= data.js ??

<script type="text/javascript" src="data.js"></script>

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.