-1

I have a JSON file in my folders. I want to change the title value in index.html with the data in the JSON file.

I have a folder and there are 8 JSON files in this folder.

  • de.json

  • en.json

  • es.json

  • fr.json

The names of the variables in the JSON file are the same, but the content is different.

"concurrent-deviceManagement-manage": "Device Management"

"concurrent-deviceManagement-manage": "Situngsverwaltung"

"concurrent-deviceManagement-manage": "Gestion des sessions"

I want to replace the title data in the HTML with the content of the "concurrent-deviceManagement-manage" variable in this JSON file.

Lordke
  • 15
  • 4
  • Does this answer your question? [How to dynamically change a web page's title?](https://stackoverflow.com/questions/413439/how-to-dynamically-change-a-web-pages-title) – E. Zacarias Dec 29 '22 at 11:39
  • What have you already tried? Loading data from a .json file is pretty easy both in plain JS and in jQuery and turns that JSON into "just another JS object to work with" so where in this multi-step process (load json, get data from resultant object, update title) did you get stuck? – Mike 'Pomax' Kamermans Dec 31 '22 at 16:28
  • @Lordke did your issue sorted? – Sachith Wickramaarachchi Dec 31 '22 at 16:46

1 Answers1

1

You can use getJSON() and do something like this,

$.getJSON("de.json", function(data) {
  $("title").text(data["concurrent-deviceManagement-manage"]);
});

Updated: With modern JS

As per the @Mike 'Pomax' Kamermans suggestion, we can do something using like this:

fetch("de.json")
    .then(response => response.json())
    .then(data => {
        document.title = data["concurrent-deviceManagement-manage"];
    });

Read more about fetch()

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68