#WioTerminal – Parsing JSON data from an #Azure ☁️ Function !

Hi !

In today’s post I’ll share an scenario about:

Invoke an Azure Function to get JSON data, and Parse the JSON data

Yesterday I wrote on how to invoke an Azure Function and get JSON data for my Digital Twin scenario. The response from the function is similar to this one:

{
    "state": 0,
    "desc": "closed",
    "source": "blog"
}

There are several libraries to work with JSON in the Arduino ecosystem, I’m using this one and so far, so good.

GitHub – Arduino_JSON

The code is super easy to read

  • Include Arduino_JSON.h in the include section
  • Parse the string with the Azure Function response, this creates a JSONVar object
  • Validate for a valid JSON object
  • Validate JSON properties to get state, description and source
// Include section
#include <Arduino_JSON.h>

// Azure Function and Door Information
int    state = -1;
String state_desc = "undefined";
String state_source = "undefined";

void getDoorStateInformation(String response){
  // init default values
  state = -1;
  state_desc = "undefined";
  state_source = "undefined";  

  // process JSon Response
  JSONVar myObject = JSON.parse(response);
  
  if (JSON.typeof(myObject) == "undefined") {
    Serial.println("Parsing input failed!");
    return;
  }
  
  if (myObject.hasOwnProperty("state")) {
    state = (int)myObject["state"];
  }
  if (myObject.hasOwnProperty("desc")) {
    state_desc = myObject["desc"];
  }
  if (myObject.hasOwnProperty("source")) {
    state_source = myObject["source"];
  }
  
  Serial.print("state: ");
  Serial.print(state);
  Serial.print(" - desc: ");
  Serial.print(state_desc);
  Serial.print(" - source: ");
  Serial.println(state_source); 
}

I’m close to finish the Wio Terminal application. Next step, let’s interact with the Digital Twin using the Wio Terminal buttons.

Happy coding!

Greetings

El Bruno


References


¿Con ganas de ponerte al día?

En Lemoncode te ofrecemos formación online impartida por profesionales que se baten el cobre en consultoría:

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: