Trouble parsing Json

Hi all,

I don't get why what I implement doesn't work.
Any idea?

I would like to retreive "lat" and "lon" from this Json.

This is what the JSON looks like : 

{
"associated":{"utcOffset":2,
		"units":{"temperature":"C","tideHeight":"M","swellHeight":"M","waveHeight":"M","windSpeed":"KPH"},
		"tideLocation":{"name":"Les Sables d'Olonne","min":-0.16,"max":6.09,"lon":-1.7833,"lat":46.5,"mean":0}
	       },

...

}

And that's what I implemented : 

	 // INIT LAT AND LON 
		 	var associated = data["associated"];
		 	var tideLocation = associated["tideLocation"];
		 	var Lat = tideLocation["lat"];
	    	var Lon = tideLocation["lon"];

Where "data" is the JSON pushed as a parameter of the onReceive function.

Any advice?

  • At this point it's not JSON, it's a dictionary.

  • For example, here's what I see in onBackgroundData() when I request JSON weather data.  It's what I get when I receive data.

    {observations=>[{lat=>38.860001, obsTimeUtc=>2021-10-12T13:49:08Z, lon=>-94.795998, realtimeFrequency=>null, country=>US, uv=>0.000000, humidity=>80.000000, qcStatus=>1, neighborhood=>Olathe, epoch=>1634046548, imperial=>{windSpeed=>0.000000, elev=>1089.000000, dewpt=>52.900002, precipTotal=>0.000000, heatIndex=>59.000000, pressure=>30.180000, temp=>59.000000, precipRate=>0.000000, windChill=>59.000000, windGust=>0.000000}, obsTimeLocal=>2021-10-12 08:49:08, softwareType=>AMBWeatherV4.2.9, solarRadiation=>69.699997, stationID=>KKSOLATH274, winddir=>89}]}

    add a println in your onReceive to display the data.

  • Hi Jim,

    Actually I post this because I tried to follow the same logic as what I learned previously from your feedback, but I struggle in adjusting my code to make it work here.

    var associated = data["associated"];
    Sys.println(associated);

    returns

    {units=>{windSpeed=>KPH, waveHeight=>M, tideHeight=>M, temperature=>C, swellHeight=>M}, tideLocation=>{min=>-0.160000, max=>6.090000, lat=>46.500000, mean=>0, name=>Les Sables d'Olonne, lon=>-1.783300}, utcOffset=>2}

    I would like to isolate tideLocation to retrieve its "Lon" and "Lat" keys.

  • Again, understans that when you do a makeWebRequest and request JSON data, you'll see a dictionary in CIQ, and given what I see in what you posed, it will be

    assocaued[tideLocations][lat];

    for example

  • Can you explain what part isn't working here? Does your code crash? If it doesn't crash what happens? (How do you know it isn't working?) (I ask because the code looks okay to me.)

    What happens if you print the values of the variables Lat and Lon?

  • Hi,

    Actually I get an error code -403 response everytime something is wrong in my code. Because of that I kindda struggle in finding what's going on.

    Thanks to your help I managed to add those two values to an array and send it to the app via Background.exit(FilteredData);

    Still when I try to store both of this values from the App, to be able to use them in the View I get this -403 returned by onStop. 
    I wonder if it could come from the fact the value is minus something?

    I treat these values (#35 and #36) the same way as #33 and #34. However here there is something going wrong somewhere : 

    //[33 - 34] CALCULATION COEFFICIENT MAREE
    					if((data[33] != null) & (data[34] != null) ){
    																HighTideHeight = data[33].toFloat();
    																//Sys.println("HighTideHeight pour Coeff: " +  HighTideHeight);
    																LowTideHeight = data[34].toFloat();
    																//Sys.println("LowTideHeight pour Coeff: " +  LowTideHeight);
    																
    																var CoeffMareesValue = ((HighTideHeight - LowTideHeight)*100) / MarnageMoyen;
    																CoeffMarees = CoeffMareesValue.toNumber();
    																
    																Storage.setValue("CoeffMarees", CoeffMarees);
    																
    																}						
    											
    										
    				
    					
    //[35 - 36] SUNRISE SUNSET HANDLING
    					if((data[35] != null) & (data[36] != null) ){/*Sys.println("data[35] -> " + data[35]);
    																*/
    																
    																
    																LatRetreive = data[35];
    																LonRetreive = data[36];		
    																
    																var Lat = LatRetreive.toFloat();
    																var Lon = LonRetreive.toFloat();	
    																
    																Storage.setValue("Lat", Lat);
    																Storage.setValue("Lon", Lon);
    																}
    

  • The response is too large

    NETWORK_RESPONSE_OUT_OF_MEMORY -403

    API Level 3.0.0

    Ran out of memory processing network response.

  • But what I don't get is the webrequest settings don't change so the size of the response should be more or less constant.
    I send 3 requests in a raw, it must be coming from there.

    Also, it's closely related to the number of prints I request in my code. If I put too many of them it triggers the -403 error...

    I can get it to work (pretty well actually) by just commenting what's in the  ""if((data[35] != null) & (data[36] != null)"".
    I'll never get this -403 error code (logged that over several days)

  • you have a max of 32k (really 28k) for background services on most devices, and that's both code and data, so if you're getting data from 3 requests, that could throw you over the max.

  • Morning Jim,

    EDIT
    I eventually managed to get the data squeezed into the array I'm returning to the App.
    However it seems that their is another issue with the code size on the App side.

    Again I treat the data exactly as what is being done upper in the app code, so I guess the implementation is probably good. However I still get this -403 error, only when I uncomment this part of the code in the App : 

    //[35 - 36] SUNRISE SUNSET HANDLING
    		if((data[35] != null) & (data[36] != null) ){
    													//nothing here for now
    													}

    I get the same by only trying a 

    Sys.println("data[35] -> " + data.size());