0

I have a bunch of objects like this:

"value": {
  "something": true,
  "another": false,
  "testing": true,
  "bob": "Hello",
  "restrooms": true,
  "price_rating": 1,
}

What I simply want to do is render the items inside value as HTML. Sort of like:

"something": true
"another": false
"testing": true
"bob": "Hello"
"restrooms": true
"price_rating": 1

Is there a simple way to do this without writing some complicated each and appending each item one at a time? Of note, these objects will have different attributes and number of attributes.

P.S.
  • 15,970
  • 14
  • 62
  • 86
jonmrich
  • 4,233
  • 5
  • 42
  • 94

4 Answers4

3

Convert the object to pretty string using JSON.stringify(obj, null, 2) and output the content to a <pre> tag

var a = {
  "value": {
    "something": true,
    "another": false,
    "testing": true,
    "bob": "Hello",
    "restrooms": true,
    "price_rating": 1,
  } 
};
document.getElementById("output").innerHTML = JSON.stringify(a.value, null, 2);
<pre id="output"></pre>
1

You can convert any object to json string format

JSON.stringify(x.value)
Ali
  • 1,982
  • 1
  • 15
  • 16
1

If you need it as html without json braces and quotes can do something like:

let data = {
    "value": {
      "something": true,
      "another": false,
      "testing": true,
      "bob": "Hello",
      "restrooms": true,
      "price_rating": 1,
    }
  },
  
  displayObj = data.value,
  
  html = Object.keys(displayObj)
  .map(key => key + ': ' + displayObj[key].toString()).join('<br>')

document.body.innerHTML = html
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0

You can convert the object into a string and use replace function.

var jsonobj = {"value": {
  "something": true,
  "another": false,
  "testing": true,
  "bob": "Hello",
  "restrooms": true,
  "price_rating": 1,
}};

var str = JSON.stringify(jsonobj.value);

str = str.replace(/,/g, '<br/>').replace('{', '').replace('}', '');

document.getElementById("output").innerHTML = str;
<div id="output"></div>
henrique romao
  • 560
  • 1
  • 8
  • 32