How to derive required fields from the JSON?

Good time of day.

On .txt file contains the Json data
{"artist":"Eminem","title":"Rap God","status":"1"}

For example, I want to display only artist And title, status not necessary to me.

you want to display on the page in div and refresh say every 5 seconds using setInterval

<div id="artist"></div>
<div id="title"></div>


js code (helped yesterday)

var json = 'https://www.site.com/json.txt'
function init(){
 let _json = JSON.parse(json)
//somewhere here I need to make a choice, but that only did not try, does not work.
 let artist = document.getElementById('artist');
 let title= document.getElementById('title');
 artist.innerHTML = _json.artist;
 title.innerHTML = _json.title;
setInterval(init,1000)
}
setInterval(init,1000)


I would be very grateful, thanks in advance.
April 3rd 20 at 18:28
1 answer
April 3rd 20 at 18:30
Try that!
var url_json = 'https://www.site.com/json.txt';
function init(){
fetch(url_json)
 .then(response => response.json())
 .then(_json => {
 let artist = document.getElementById('artist');
 let title= document.getElementById('title');
 artist.innerHTML = _json.artist;
 title.innerHTML = _json.title;
});
}
setInterval(init,1000);

OR
var url_json = 'https://www.site.com/json.txt';
function init(){
fetch(url_json)
 .then(response => response.json().then(_json => {
 let artist = document.getElementById('artist');
 let title= document.getElementById('title');
 artist.innerHTML = _json.artist;
 title.innerHTML = _json.title;
}));
}
setInterval(init,1000);

In your example, the error - JSON.parse() parses a JSON string and you are passing is not JSON string, and url

Find more questions by tags JSONParsing