How to repalce HTML text with JSON data

Received data from api and converted into JSON format, however every time I try to change a selected element in HTML I get either undefined or Object Obejct.

I have tried JSON.parse/JSON.stringify. I have tried innerHTML, innerText, textContent. I have tried for loops.

HTML
<p id="lang">C++</p>
Javascript
let language = document.getElementById('lang');

let data = {
   "book": [

      {
         "id":"01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt"
      }
   ]
};

data = JSON.stringify(data);

language.innerHTML = data.book.language;

Need C++ in paragraph tag to change to Java in data. If Jquery would make this easier I would appreciate knowing how that works as well.

Since the data model you presented has books as an array, you'll need to get that out this way

data.book[0].language

0 being the index of whatever book you want to display

data = {
   "book": [

      {
         "id":"01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt"
      }
   ]
};

language.innerHTML = data.book[0].language;

Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML.

This is not working as book is an array of objects, not a singular object. So you'd need to use data.book[0].language

P.S. You don't need to use JSON.stringify here, if you are receveing a JSON string from your API, you should use JSON.parse

Here is an example of the code running correctly:

https://jsfiddle.net/2et3zh7g/

Replace "blue" of eye by "green", "blue" of favorite color by "red" and "male" by "female". When the page will be loaded, I want to see, for instance Green (not Blue) for Eye and Female for Gender (not Male). I guess I need to use functions next: GM_getValue() GM_setValue() JSON.parse() JSON.stringify() PS: the code JSON is directly in the page

From Mozilla

The JSON.stringify() method converts a JavaScript object or value to a JSON string

So it seems your code is going the opposite direction of your stated intent.

let data = {
  "book": [
    {
       "id":"01",
       "language": "Java",
       "edition": "third",
       "author": "Herbert Schildt"
    }
  ]
};

After which, data already contains an object.

data = JSON.stringify(data);

And now, data contains a string. So naturally, data.book will result in an undefined value.

If, however, you were to receive the data as actual JSON, such as

let dataJSON = '{"book":[{"id":"01","language":"Java","edition":"third","author":"Herbert Schildt"}]}';

You could then extract the language value using JSON.parse as follows:

let data = JSON.parse(dataJSON);
let bookLanguage = data.book[0].language;
language.innerText = bookLanguage;

Note the array subscript on book. Since in your example, it contains an array of objects, you need to be sure to subscript into it.

To use it just place the text into the box above and select how your text is broken-up to allow us to build the JSON according to your needs. Your text can be split by line breaks, commas, semi-colons or by spaces. If you want to have an output in which each JSON data item is unique check the "remove duplicate entry names" checkbox.

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

This article shows how to use the System.Text.Json namespace to serialize and deserialize to and from JavaScript Object Notation (JSON). If you're porting existing code from Newtonsoft.Json, see How to migrate to System.Text.Json. The directions and sample code use the library directly, not through a framework such as ASP.NET Core.

JavaScript Object Notation (JSON) is a lightweight, text-based, language-independent data interchange format. It was derived from the ECMAScript Programming Language Standard. JSON defines a small set of formatting rules for the portable representation of structured data.

Comments
  • When parsed, data.book[0].language, the object containing language property is in an array.
  • data is not JSON. It's an object. JSON is a text format for storing or passing data.