How to store nested json key value?

I am struggling to find this in the forum and docs.

I am trying to get rssi and bars values from card.wireless and store it in two variables. I see it is in “net”

{“status”:“{network-up}”,“count”:4,“net”:{“iccid”:“….”,“modem”:“BG95M3LAR02A03_01.006.01.006”,“band”:“GSM 900”,“rat”:“gsm”,“rssir”:-61,“rssi”:-62,“bars”:4,“mcc”:274,“mnc”:2,“lac”:102,“cid”:20441}}

my code is like such, I assume it does not peek into nested key value pair.

double signal_rssi = 0;
double signal_bars = 0;
rsp = notecard.requestAndResponse(notecard.newRequest(“card.wireless”));
if (rsp != NULL)
{
signal_rssi = JGetNumber(rsp, “rssi”);
usbSerial.print(signal_rssi);
signal_bars = JGetNumber(rsp, “bars”);
usbSerial.print(signal_bars);
notecard.deleteResponse(rsp);
}

Bonus question, is there another better way to get this data out on a note? Than fetching with card.wireless and loading into note.add

Hi @Embedded_iceman and welcome to the Blues community!

You’re looking for the Parsing JSON Objects section of the note-arduino docs. Specifically, your response object contains nested objects that need to be parsed with JGetObject. Something like this code:

J *net = JGetObject(rsp, "net");
int bars = JGetNumber(net, "bars");

Hope that helps!
Rob

1 Like

Thanks Rob,
Here is my corrected code if someone runs into the same in the future

   double signal_rssi = 0;
   double signal_bars = 0;
  rsp = notecard.requestAndResponse(notecard.newRequest("card.wireless"));
  if (rsp != NULL)
  {
    signal_rssi = JGetNumber(JGetObject(rsp, "net"), "rssi");
    signal_bars = JGetNumber(JGetObject(rsp, "net"), "bars");
    notecard.deleteResponse(rsp);
  }
2 Likes