JSON Parser?

Hi,

I want to create a „status“ page from a string variable formatted as a JSON value. Using IFTTT a string variable in IPS is updated when an action has been executed. This JSON value contains the values I need, but different actions create different JSON values: Is there some easy method to parse the JSON string variable? Here are some examples of the JSON string variable:

{"key_name_user":"","key_account_email":"","requested_state":"DAY_LOCK","event_type":"STATE_CHANGED_LATCH","value2":"IFTTT","key_name_admin":"IFTTT","value1":"LockID: xxxx, KeyID: x, State: DAY_LOCK","value3":"","key_account_name":"","key_local_id":x,"lock_id":xxxx}
{"key_name_user":"somedoor","key_account_email":"user@gmail.com","requested_state":"NIGHT_LOCK","event_type":"STATE_CHANGED_NIGHT_LOCK","value2":"User","key_name_admin":"User","value1":"LockID: xxxx, KeyID: x, State: NIGHT_LOCK","value3":"user@gmail.com","key_account_name":"somedoor","key_local_id":x,"lock_id":xxxx}
{"go_to_state":"NIGHT_LOCK","key_name_user":"somedoor","key_account_email":"user@gmail.com","event_type":"STATE_CHANGED_NIGHT_LOCK_REMOTE","value2":"User","key_name_admin":"User","value1":"LockID: xxxx, KeyID: x, State: NIGHT_LOCK","value3":"user@gmail.com","key_account_name":"somedoor","key_local_id":x,"lock_id":xxxx}
<?php
$json = '{
   "key_name_user":"",
   "key_account_email":"",
   "requested_state":"DAY_LOCK",
   "event_type":"STATE_CHANGED_LATCH",
   "value2":"IFTTT",
   "key_name_admin":"IFTTT",
   "value1":"LockID: xxxx, KeyID: x, State: DAY_LOCK",
   "value3":"",
   "key_account_name":"",
   "key_local_id":"x",
   "lock_id":"xxxx"
}';
$json_arr = json_decode($json, true);
$value1 =  explode(',', $json_arr['value1']);
$value1_arr = array();
foreach($value1 as $v){
    $t = explode(":", $v);
    $value1_arr[trim($t[0])] = trim($t[1]);
}
print_r($value1_arr);
$state = $value1_arr['State'];

SetValue($ipsID, $state);

Thanks!

I’m not sure what to do with this line: „SetValue($ipsID, $state);“

Notice: Undefined variable: ipsID
Warning: Variable #0 does not exist

You have to create an IP-Symcon Variable from Type ‚String‘ and insert instead of $ipsID the 5-digits Object-ID.

That’s what I thought I should do, let’s try again. :wink:
I probably did something wrong before, on the second try it works as expected.

@tobiasr ,

Surely my PHP skills needs to be improved. Could you tell me how I can „rewrite“ $state values? Something like this:
„OPEN“ = „Open“
„DAY_LOCK“ = „Unlocked“
„NIGHT_LOCK“ = „Locked“
„UNKNOWN“ = „Unknown“

You can solve this by using the IP-Symcon build-in Variable Profiles.

I’ve should have thought of that myself. Thanks! :wink: