Piston data from JSON URL from Tesla


#1

Hey guys,

First off, I have many pistons working fine for collecting data from JSON, my issue is the format of the response from Tesla differs to what I’m used to.

Normal Response I can handle
{“access_token”:“abc”,“token_type”:“bearer”,“expires_in”:3888000,“refresh_token”:“def”,“created_at”:1520401929}

I then set a variable based on say {$response.access_token}

The issue I have is once Authenticated with Tesla, I try fetch data from them such as the cars charging state, the response I get back has a “response:” then a sub set of data. I tried using {$response.response.charging_state} but that didn’t work. How do i get a variable from the below values when its not in the main response?

Example response below I’m trying to get a variable from

{“response”:{“charging_state”:“Stopped”,“fast_charger_type”:“ACSingleWireCAN”,“fast_charger_brand”:"\u003cinvalid\u003e",“charge_limit_soc”:61,“charge_limit_soc_std”:90,“charge_limit_soc_min”:50,“charge_limit_soc_max”:100,“charge_to_max_range”:false,“max_range_charge_counter”:0,“fast_charger_present”:false,“battery_range”:125.87,“est_battery_range”:92.57,“ideal_battery_range”:100.81,“battery_level”:36,“usable_battery_level”:36,“charge_energy_added”:0.0,“charge_miles_added_rated”:0.0,“charge_miles_added_ideal”:0.0,“charger_voltage”:0,“charger_pilot_current”:32,“charger_actual_current”:0,“charger_power”:0,“time_to_full_charge”:3.83,“trip_charging”:false,“charge_rate”:0.0,“charge_port_door_open”:true,“conn_charge_cable”:“IEC”,“scheduled_charging_start_time”:1520424000,“scheduled_charging_pending”:true,“user_charge_enable_request”:null,“charge_enable_request”:false,“charger_phases”:null,“charge_port_latch”:“Engaged”,“charge_current_request”:32,“charge_current_request_max”:32,“managed_charging_active”:false,“managed_charging_user_canceled”:false,“managed_charging_start_time”:null,“battery_heater_on”:false,“not_enough_power_to_heat”:false,“timestamp”:1520402547405}}


#2

Possible option (bit of a long shot):

{replace($response, ‘“response”:’ , ‘’)).charging_state}


#3

Thanks for your attempt :slight_smile:
I added
replace($response, ‘“response”:’ , ‘’).lattitude

I get an error saying “Variable .latitude not found (create it)” so it doesn’t like the last part.
I let it run anyway and i see the variable was set to “[:]Variable ‘.lattitude’ not found”


#4

Sorry then… I’m not sure.

Spent a bit of time playing with your response using mid() replace() and arrayitem() with no joy.

Returns invalid ternary operator for most of my attempts :sob:


#5

It appears i need to work with the nested JSON, does anyone know if webcore can deal with nested JSON?


#6

I believe what you’re looking for is $response.response.charging_state. E.g.:

3/8/2018, 9:34:38 PM +331ms
+84ms	║[response:[charging_state:Stopped]]
+98ms	║[charging_state:Stopped]
+104ms	║Stopped

I had to escape the braces here since I was entering it as a WC string, but that’s not an issue for the JSON parsing that happens as part of WC’s web request.


#7

Still can’t get it to work, found another nested JSON im trying to work with at http://01e6b0.ethosdistro.com/?json=yes


#8

figured it out thanks for your example, incase anyone else has the same issue…


#9

Just trying it on the original JSON and still having issue with that one…

{“response”:{“shift_state”:“P”,“speed”:null,“power”:-6,“latitude”:-37.700534,“longitude”:176.106509,“heading”:80,“gps_as_of”:1520843556,“native_location_supported”:1,“native_latitude”:-37.700533,“native_longitude”:176.10651,“native_type”:“wgs”,“timestamp”:1520843557634}}

I get error
║║Error parsing JSON data [response:[timestamp:1520844156981, native_type:wgs, native_longitude:176.15051, native_location_supported:1, shift_state:P, speed:null, power:-6, longitude:176.150509, gps_as_of:1520844156, latitude:-37.700534, native_latitude:-37.700533, heading:80]]

Any idea whats wrong with the output?


#10

I think some sits require the parse JSON and others done, weird


#11

Sorry if I confused you. I used the Task “Parse JSON data…” and the variable $json because I couldn’t actually use the same GET request as you. FYI, when you do a GET (or PUT…), the response is automatically parsed into $response. This is why I said what you were looking for was $response… (not $json… I only used $json because of the way I was testing.)

So there are two ways to parse JSON data:

JSON string —> Parse JSON data… —> $json

or

Make web request —> response automatically parsed —> $response

To be clear, in this example piston you can delete the “Parse JSON data {$response}” task and change the log task from $json.rigs.294350.hash to $response.rigs.294350.hash. (Otherwise you’re trying to parse twice, which is why it’s not working.)