0

I want to use variables to get a specific value from my JSON.

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
  var jsonContent = JSON.parse(this.responseText);

  var day = mon;
  var name = abc;

  var restaurants = jsonContent.name.table.day;

The variables "day" and "name" will be different everytime the code starts because I need different values from my JSON.

How can I solve this?

Linus
  • 3
  • 2
  • 1
    Use bracket notation: `jsonContent[name]['table'][day]`. https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets – Lain Dec 06 '19 at 13:28
  • FYI, after the `JSON.parse`, you are no longer dealing with JSON, you have a JavaScript Object. – crashmstr Dec 06 '19 at 13:30

1 Answers1

1

Get values with property string like this

var restaurants = jsonContent[name].table[day]
Mustafa Arslan
  • 774
  • 5
  • 13
  • This one works. Just needed to change "var day = mon;" and "var name = abc;" to string values. Thanks! – Linus Dec 06 '19 at 13:35