0

Using a fetch request to get some data. The JSON object path depends on a string variable. How would I convert the string into an object that can be referenced as an array in the path. Here's my code:

var path = 'bob';

fetch(request)
    .then(function(response) {
        if (!response.ok) {
            throw Error(response.statusText);
        }
        return response.json();
    }).then(function(json) {

        var data = json.message.path[0].text;

    }).catch(function(error) {
        console.log(error);
    });

JSON:

{
"message": {
    "bob": [
      {
        "name_id": "3351",
        "name": "bob",
        "text": "lorem ipsum"
      },
      {
        "name_id": "34562",
        "name": "bob",
        "text": "lorem ipsum2"
      }
    ]
}

Basically, path defines the correct object to be used in the dot notation. BUT - the object also has to be able to reference the array.

Any solutions?

bounty
  • 409
  • 1
  • 4
  • 8
  • What do you mean by "the object has to be able to reference the array"? Can you give a concrete example of the structure of `json` and `path`? – qxz Aug 07 '16 at 00:08
  • The the JSON is organized, the path is an array and I need to get the first item in that array. But I can only reference the path by a string. See example code. – bounty Aug 07 '16 at 00:10
  • In your example, `path` isn't an array, it's a string... Do you mean that you get a string representing a path, which you then want to convert into an array? – qxz Aug 07 '16 at 00:13
  • Adding context/more specifics to your question will help. – qxz Aug 07 '16 at 00:13
  • Possible duplicate of [Dynamically access object property using variable](http://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – melpomene Aug 07 '16 at 00:13
  • Added the example JSON. Basically access 'bob' at the top level is determined by a string. But I need to be able to reference bob[0] in the object notation when I'm defining the data. – bounty Aug 07 '16 at 00:17
  • Sorry, edited example. `path = 'bob'` `path` is the variable being predefined in the code in order to reference the correct data. – bounty Aug 07 '16 at 00:19
  • OK, now it's just a direct duplicate. – melpomene Aug 07 '16 at 00:21
  • But what would be the syntax for using it as an array? `json.message[path][0].text`? – bounty Aug 07 '16 at 00:22
  • 1
    Yes, that's how you use arrays. Or `var tmp = json.message[path]; ... tmp[0]` - same result. – melpomene Aug 07 '16 at 00:24

2 Answers2

1

You could try using:

var data = json.message[path][0].text;

where path is the name of the object as you defined:

path = "bob";
Akintayo Jabar
  • 836
  • 8
  • 13
0

If you have a path variable in the format user.index, you could reference the corresponding message in your JSON like this:

path = path.split(".");
var name = path[0], index = path[1];
var data = json.message[name][index].text;

If path is "bob.1", data would become "lorem ipsum2"

json.message[name] evaluates to the array of messages. To index into that array, just put brackets after the value, like any other array: json.message[name][index], which is equivalent to (json.message[name])[index], which is equivalent to var array = json.message[name] then array[index].

Note that this solution does no error checking, and will throw an error if the given user isn't in json.message.

qxz
  • 3,814
  • 1
  • 14
  • 29