0

I have some json data that I need to display on my app.component.html

Here is some sample data:

[
  {
    "name": "d1",
    "days": [
      "monday",
      "wednesday",
    ],
    "options": {
      "name": "o1",
      "extras": [],
      "temp": [
        "12",
        "25",
        "12"
      ]
    }
  },
  {
    "name": "d2",
    "days": [
      "tuesday",
      "wednesday",
    ],
    "options": {
      "name": "o2a",
      "extras": [
        {
          name: 'extra 1'
        }
      ],
      "temp": [
        "22",
        "25",
        "12"
      ]
    }
  }
]

At the moment I have it this way:

<ul *ngFor="let dat of data">
  <li>{{dat.name}}</li>
</ul>

..etc

But the key names and values are not fixed so they can change and be more or less.

My question is, how can I do this where it can read any json data?

Tomazbyden2000
  • 83
  • 3
  • 12
  • Can you show what you expect the generated html to become with the sample data? In the case of `name`, it's easy: you just want it inside the `
  • `, but in the case of `days` and `options`, it's not very clear
  • – ShamPooSham Jul 09 '20 at 18:00
  • @ShamPooSham, What I understood is, the user's JSON key names are not final but he/she wants to ensure the data gets displayed without breaking even if the key names gets changed in future. So, I was suggesting that it may not be possible. – Akhil Jul 09 '20 at 18:04
  • 1
    so you're basically asking how do I keep displaying a chicken if tomorrow they call it a cow. I'm not sure its possible unless you're willing to show the whole farm using `
     data | json
    `
    – Stavm Jul 09 '20 at 18:18
  • you can try `recursive template` there are many examples. https://stackoverflow.com/questions/35733186/angular2-ul-li-json-tree-recursive-in-ngfor or https://steemit.com/utopian-io/@jaysermendez/angular-tricks-recursively-rendering-a-tree-structure it depends how enhanced view you are need – Radik Jul 09 '20 at 18:54