Looping over JSON after receiving it

I have a JSON packet, which has an object, which then has an array. I want to loop over that, in standard JS this would be easy enough, how do I go about achieving it here?

I’m using FetchJson which returns an Observerable object, so I’m accessing it via “value”, I don’t know if this is correct.

for (var i = 1; i<data.value.heroes.length; i++) { debug_log(i); }

The error is a type error, “undefined cannot be converted into an object”

Here is my JSON: http://noms.me.uk/heroes.json

Cheers

Hi!

FetchJson is asynchronous, so the value will not be available immediately. It is only meant for reactive programming, with operators such as .map() and then databinding the result.

If the result is not databound, the fetch will not be performed.

If you want to write imparative code in response to a HTTP request in Fuse, you can do it the “standard JS” way, with XMLHttpRequest instead :slight_smile:

PS: The even more convenient fetch(url).then(.. API is also on the way in a near future release :slight_smile:

Hey,

So if I wrap my for loop inside the addSubscriber method would this be a viable way of doing it?

Loving the product so far, very quick for prototyping, trying to get my head around this data binding stuff

One more thing - is there any other way apart from debug_log to log an object? It just comes back as JurrassicObject

Thanks!

You can do debug_log(JSON.stringify(yourObj))

That’s great thanks - so if I can JSON.stringify an object, why can’t I JSON.parse it?

      var parsed = JSON.parse(data.value);

Compile error of "unexpected keywork ‘undefined’ at line 0?

That should work, maybe you have an error in your json data?

I have to wrap it in

if (data.value != undefined) {
  var parsed = JSON.parse(data.value);
}

Since the function in addSubscriber gets called twice. One with undefined in data.value, and one with the actual data.