JSON help please

Oct 16, 2018
1,924
6,531
Florida, USA
I would really appreciate some examples on using JSON to control Blue Iris.

I am trying to write some scripts in vb.net and can't seem to get started.

Thanks in advance,

Michael
 
Here's a redacted version of what I use in PHP.
Hope it helps you get started.

PHP:
# *********************************************************************
# INITIALIZE THE BLUE IRIS web server AND IMPORT SETTINGS
# Multiple calls are made to the BI web server JSON interface.
#   0) jsonCmd => login     ... get the session id
#   1) jsonCmd => login     ... get schedule & profile arrays + other server settings
#   2) jsonCmd => status    ... get current shield, global schedule, profile, lockstate + more
#   3) jsonCmd => sysconfig ... get schedule and archiving states
#   4) jsonCmd => camlist   ... get all camera lists (including groups)

# 0) Initialize the web server session ID and login hash.
$jsonCmd = array(
    'cmd' => 'login'
);
$jsonResponse = get_json2($host, $jsonCmd, $debug_flg);
if ($jsonResponse === null) {  # Abort now if the Blue Iris web server is not available.
    die("Null Response: web server is *not* responding!");
}
$jsonResponseDecode = json_decode($jsonResponse, TRUE);
if (!isset($jsonResponseDecode['session'])) {
    # Abort now if the Blue Iris web server is not returning expected Json response.
    # If untrapped, PHP returns error "Undefined Index"
    die("Web server returned an empty response!");
}
$session = $jsonResponseDecode['session'];
$hash = md5($user.":".$session.":".$password);

# 1) Get schedule & profile arrays + other server settings.
$jsonCmd = array(
    'cmd' => 'login',
    'session' => $session,
    'response' => $hash
);
$jsonResponse = get_json2($host, $jsonCmd, $debug_flg);
$jsonResponseDecode = json_decode($jsonResponse, TRUE);
$bi_system_name = $jsonResponseDecode['data']['system name'];
$bi_admin = $jsonResponseDecode['data']['admin'];
$bi_ptz = $jsonResponseDecode['data']['ptz'];
$bi_audio = $jsonResponseDecode['data']['audio'];
$bi_clips = $jsonResponseDecode['data']['clips'];
$bi_streamtimelimit = $jsonResponseDecode['data']['streamtimelimit'];
$bi_dio = $jsonResponseDecode['data']['dio'];
$bi_version = $jsonResponseDecode['data']['version'];
$bi_license = $jsonResponseDecode['data']['license'];
$bi_support = $jsonResponseDecode['data']['support'];
$bi_user = $jsonResponseDecode['data']['user'];
$bi_latitude = $jsonResponseDecode['data']['latitude'];
$bi_longitude = $jsonResponseDecode['data']['longitude'];
$bi_tzone = $jsonResponseDecode['data']['tzone'];
$bi_streams = $jsonResponseDecode['data']['streams'];
$bi_sounds = $jsonResponseDecode['data']['sounds'];
$bi_www_sounds = $jsonResponseDecode['data']['www_sounds'];
$bi_profiles = $jsonResponseDecode['data']['profiles'];
$bi_schedules = $jsonResponseDecode['data']['schedules'];

# 2) Get current shield (signal), global schedule, profile, lockstate + more.
$jsonCmd = array(
    'cmd' => 'status',
    'session' => $session
);
$jsonResponse = get_json2($host, $jsonCmd, $debug_flg);
$jsonResponseDecode  = json_decode($jsonResponse, TRUE);
$bi_signal = $bi_signals[$jsonResponseDecode['data']['signal']];
$bi_schedule = $jsonResponseDecode['data']['schedule'];
$bi_profile = $jsonResponseDecode['data']['profile'];
$bi_profileName = ($bi_profile == -1 ? "None" : $bi_profiles[$bi_profile]);
$bi_lockstate = $jsonResponseDecode['data']['lock'];
$bi_lockstateName = $bi_lockstates[$bi_lockstate];
# Note: Other properties available in JSON 'status' cmd include:
#    [cxns], [cpu], [mem], [memfree], [memload],
#    [disks] => Array ([disk] ,[allocated], [used], [free], [total]),
#    [dio] => Array([0] .. [7]),
#    [uptime], [clips], [warnings], [alerts], [tzone]


# 3) Get schedule and archiving states.
$jsonCmd = array(
    'cmd' => 'sysconfig',
    'session' => $session
);
$jsonResponse = get_json2($host, $jsonCmd, $debug_flg);
$jsonResponseDecode = json_decode($jsonResponse, TRUE);
if ($jsonResponseDecode['result'] === "success") {
    $bi_schedule_state_flg = $jsonResponseDecode['data']['schedule'];
    $bi_schedule_state = ($bi_schedule_state_flg ? "On" : "Off");
    $bi_archving_state_flg = $jsonResponseDecode['data']['archive'];
    $bi_archving_state = ($bi_archving_state_flg ? "On" : "Off");
} else {
    $bi_schedule_state = null;
    $bi_archving_state = null;
}

# 4) all camera lists (including groups).
$jsonCmd = array(
    'cmd' => 'camlist',
    'session' => $session
);
$jsonResponse = get_json2($host, $jsonCmd, $debug_flg);
$jsonResponseDecode = json_decode($jsonResponse, TRUE);
$json_camlist_obj = $jsonResponseDecode;  # Save for later use.

# extract array of BOTH camera short names AND camera group names.
$bi_camlist_indices_ary = array_column($jsonResponseDecode['data'],'optionValue');

# extract cam and group list arrays.
$result_ary = $jsonResponseDecode['data'];
$bi_cam_names_ary = array();
$bi_cam_longnames_ary = array();
$bi_cam_longnames_ary2 = array(); # lookup array
$bi_group_names_ary = array();
$bi_group_cams_ary = array();   # array of arrays. contains cam lists for each group name.
for ($i = 0; $i < count($result_ary); $i++) {
    $option_val = $result_ary[$i]['optionValue'];  # shortname
    $option_disp = $result_ary[$i]['optionDisplay'];  # longname
    # Extract properties
    if (!array_key_exists('group', $result_ary[$i])) {  # single cameras.
        $is_enabled = (isset($result_ary[$i]['isEnabled']) ? $result_ary[$i]['isEnabled'] : 0);
        //$is_hidden = (isset($result_ary[$i]['hidden']) ? $result_ary[$i]['hidden'] : 0);
        //if($is_enabled == 1 && $is_hidden != 1) {  # Ignore disabled & hidden cams.
        if($is_enabled == 1) {  # Ignore disabled cams.
            if (($option_val != "Index") && strpos($option_val, "@") === false) {
                array_push($bi_cam_names_ary, $option_val);
                array_push($bi_cam_longnames_ary, $option_disp);
                $bi_cam_longnames_ary2[$option_val] = $option_disp;
            }
        }
    } else {  # cam group properties.
        $grp_ary = $result_ary[$i]['group'];
        array_push($bi_group_names_ary, $option_val);
        array_push($bi_group_cams_ary, $grp_ary);
    }
}

function get_json2($host, $jsonCmd, $debug=false)
{
/**
* FUNCTION: get_json2($host, $jsonCmd, $debug=false)
* PURPOSE:  Return an encoded JSON response using cURL.
* ARGS:     $host      = (string) BI web server IP address
*           $jsonCmd   = (array) json command
*           $debug     = (boolean) true shows host address and response
* RETURNS:  $rtn_obj   = (object) web server's json response
* NOTE: The cURL options used herein have been tested to work for the Blue Iris
*       web server JSON interface.
* REFS:
* http://thisinterestsme.com/sending-json-via-post-php/
* http://stackoverflow.com/questions/16920291/post-request-with-json-body
* http://lornajane.net/posts/2011/posting-json-data-with-php-curl
*/

    $url = "http://".$host."/json";

    # Initiate cURL.
    # Build cURL options.
    $options = [
        CURLOPT_URL => $url,
        CURLOPT_POST => 1,  # send a POST request.
        CURLOPT_POSTFIELDS => json_encode($jsonCmd), # Attach encoded JSON string.
        CURLOPT_RETURNTRANSFER => true,  # return curl_exec() page as a variable
        CURLOPT_HTTPHEADER => array(
            //'Basic: 123456789',
            'Content-Type: application/json',  # Set the content type
        ),
    ];
    if ($debug) {
        //$options[CURLOPT_HEADER]  = true;
        $options[CURLOPT_VERBOSE] = true;
    }

    # Execute the request
    $ch = curl_init();
    curl_setopt_array($ch, $options);
    $jsonResponse = curl_exec($ch);

    # Show debug info.
    if ($debug){
        echo "<pre>".$url."</pre>";
        echo "<pre>"."jsonCmd:<br>".print_r($jsonCmd, true)."</pre>";
        echo "<pre>"."jsonResponse:<br>".print_r(json_decode($jsonResponse, TRUE), true)."</pre><hr />";
    }

    # Check for errors
    if($jsonResponse === FALSE) {
        # die() will output the following message if the web server is not found.
        # "Failed to connect to [address] port [port]: Connection refused"
        die(curl_error($ch));
    }
    if (strpos(strtolower(curl_error($ch)), strtolower("Connection refused")) > 0) {
        $rtn_obj = null;  # Return null if server not found.
        return $rtn_obj;
    }

    # All is good.
    $rtn_obj = $jsonResponse;
    return $rtn_obj;
}
 
Last edited: