Jump to content

Recommended Posts

How to add .json file in html document.

 

I tried like this ,but not got output

 

array.json file

 

var employees = [
{
"firstName":"John",
"lastName":"Doe"
},
{
"firstName":"Anna",
"lastName":"Smith"
},
{
"firstName":"Peter",
"lastName":"Jones"
},
];
employees[0].firstName="Gilbert";
document.getElementById("demo").innerHTML =
employees[0].firstName + " " + employees[0].lastName;
abc.html file
<!DOCTYPE html>
<html>
<head>
<script src="array.json"></script>
</head>
<body>
<h2>Create a JavaScript Object</h2>
<p id="demo"></p>
</body>
</html>

 

Link to comment
Share on other sites

are you mixing .json and JS in one file? I don't think that's going to work. I would just put it all in one JS file and run this

 

employees[0].firstName="Gilbert";document.getElementById("demo").innerHTML = employees[0].firstName + " " + employees[0].lastName;
Link to comment
Share on other sites

No error message but, but when rendering html file not adding json file

Is the path correct? You can check on the network tab in the developer tools to make sure it is requesting and finding that file. As you showed in the first post, that code will produce a fatal runtime error. I don't think it's necessary, but you might want to add a type attribute to the script tag also. I think that Javascript is the default though, so that shouldn't be necessary. It might be necessary if your server does not send a Javascript content type header for a .json file though.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...