/    Sign up×
Community /Pin to ProfileBookmark

Cannot display JSON in HTML using JavaScript

So I’ve got this JSON file (attached). I cannot get it to display in HTML when I call the function in JavaScript.
Any help?

“`const data = {
“layers”: [{
“order”: 0,
“items”: [{
“order”: 2,
“name”: “Randomly Generous Red”,
“imgSrc”: “jeb.png”
},

{
“order”: 1,
“name”: “Too Agreeable Silver”,
“imgSrc”: “VRC.png”
},

{
“order”: 3,
“name”: “Almost Whispy Blue”,
“imgSrc”: “58Z.png”
},

{
“order”: 0,
“name”: “Some Times Juicy Red”,
“imgSrc”: “081.png”
}
]
},

{
“order”: 1,
“items”: [{
“order”: 0,
“name”: “Never Substantial Silver”,
“imgSrc”: “BWK.jpg”
},

{
“order”: 1,
“name”: “Really Adorable Pink”,
“imgSrc”: “hk9.jpg”
}
]
},

{
“order”: 2,
“items”: [{
“order”: 2,
“name”: “Very Honest Black”,
“imgSrc”: “0Og.png”
},

{
“order”: 4,
“name”: “Never Eager Red”,
“imgSrc”: “2Ks.png”
},

{
“order”: 0,
“name”: “Kind Of Adorable Silver”,
“imgSrc”: “L99.png”
},

{
“order”: 3,
“name”: “Some Times Confident Pink”,
“imgSrc”: “Wx4.png”
},

{
“order”: 1,
“name”: “Never Brave Blue”,
“imgSrc”: “020.png”
}
]
}
],
“default_configuration”: [
0,
1,
4
]
};

const sorted = {
‘layers’: data.layers
.sort(({ order: a }, { order: b}) => a – b)
.map(o => {
o.items.sort(({ order: a }, { order: b}) => a – b);
return o;
}),
‘default_configuration’: data.default_configuration
};

console.log(sorted);
“`

**JAVASCRIPT:**

“`<script>

fetch(‘url’)
.then(function (response) {
return response.json();
})
.then(fucntion(data) {
appendData(data);
})
.catch(function (err) {
console.log(err);
});

function appendData(data) {
var mainContainer = document.getElementById(“myData”);
for (var i = 0; i < data.length; i++) {
var div = document.createElement(“div”);
div.innerHTML = ‘Name: ‘ + data.order + ‘ ‘ + data.name;
mainContainer.appendChild(div);
}
}

</script>
“`

Edited by site staff: Adjusted code formatting.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJul 30.2019 — First: There is a typo here: `.then(fucntion(data) {`

I put the JSON into a file and read and processed it like this:
&lt;div id="myData"&gt;&lt;/div&gt;
&lt;script&gt;
// 'thread195-read-json.json' is the name of the
// JSON file I created for testing
fetch('thread195-read-json.json')
.then(function (response) {
return response.json();
})
.then(function (data) {
appendData(data);
})
.catch(function (err) {
console.log(err);
});

<i> </i> function appendData(data) {
<i> </i> var mainContainer = document.getElementById("myData");

<i> </i> // create a sorted object from data
<i> </i> const sorted = {
<i> </i> 'layers': data.layers
<i> </i> .sort(({ order: a }, { order: b }) =&gt; a - b)
<i> </i> .map(o =&gt; {
<i> </i> o.items.sort(({ order: a }, { order: b }) =&gt; a - b);
<i> </i> return o;
<i> </i> }),
<i> </i> 'default_configuration': data.default_configuration
<i> </i> };

<i> </i> console.log(sorted);

<i> </i> // in two nested loops:
<i> </i> // read order and name from sorted data
<i> </i> // and output it
<i> </i> data.layers.forEach((layer, idx) =&gt; {
<i> </i> layer.items.forEach((item, idx) =&gt; {
<i> </i> var div = document.createElement("div");
<i> </i> div.innerHTML = 'Name: ' + item.order + ' ' + item.name;
<i> </i> mainContainer.appendChild(div);
<i> </i> });
<i> </i> });
<i> </i> }
Check if this is what you require.
×

Success!

Help @MichaelChad spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...