Reading data from a Notecard response with C/C++ API

Hi all. I have what I suspect is a simple question, and I’m probably just overlooking something obvious. I am using C with the Notecard libraries and trying to retrieve values from environment variables set up on Notehub.

When I send the query to the Notecard, I can see the response scroll on console, and it has all of the information I’m wanting to get. The only problem is that when I try to extract those fields from the JSON response, I get zeros instead of the actual values.

Here’s a piece of code I’ve been testing. To simplify troubleshooting, right now I’m just trying to get it to correctly retrieve and print to log the value of one item (filter_rssi_min).

J *req = NoteNewRequest("env.get");
J *rsp = NoteRequestResponse(req);

if (rsp)
{
    int min_rssi = JGetInt(rsp, "filter_rssi_min");
    LOG_ERR("RSSI MIN: %d\n",min_rssi);
    NoteDeleteResponse(rsp);
}

If I run this code, though, I end up with a 0 in min_rssi.

But here is the response I see coming across, showing that the data I’m looking for is indeed there:

[INFO] {“time”:1726847351,“body”:{“_tri_mins”:“1440”,“filter_rssi_min”:“-128”,“heath_status_interval”:“30”,“uploader_sync_interval”:“3”,“_fw”:“gr3_Jun21$20240621202057.binpack”,“_fw_retry”:“1719003592”,“_fwc”:“notecard-6.2.3.16585$20240517112300.bin”,“_fwc_retry”:“1719001785”,“_sn”:“scott_desk”,“_tags”:“300 Dr\r\n\r\n75602\r\nDevice ID 6789546”,“reading_interval”:“600”}}

What am I doing wrong here? Do I need to somehow provide a “path” to the value I’m looking for, to specify it’s in the body sub-object?

Thanks!
Scott

Hey @scase,

When you retrieve multiple environment variables using env.get they come back in a body object, so you need one additional level of parsing to get at the value you’re looking for.

Something like this should do the trick:

J *body = JGetObject(rsp, "body");
int min_rssi = JGetInt(body, "filter_rssi_min");

See this documentation section for a more through explanation: Arduino Library - Blues Developers

TJ

Aha! Thank you, TJ! Is the linked documentation applicable even if I am not using Arduino? I’m developing in C under Zephyr for a Nordic device, so maybe I missed out on some docs that I didn’t think applied.

Thanks again!
Scott

Our Arduino library is a small layer on top of note-c, so the majority of what’s in that guide will work for you as well.

The note-c documentation is here, but admittedly doesn’t have the greatest JSON examples: Getting Started — note-c documentation.

TJ

Hrmm, I am still getting zeros returned. Here’s what I’m doing at present. (I tried it with a different parameter this time just to rule out the possibility that the first one being negative had something to do with it.)

J *req = NoteNewRequest("env.get");
J *rsp = NoteRequestResponse(req);
J *body = JGetObject(rsp, "body");

int test_value = -222;  // init to nonzero value so I can tell if it's been set
test_value = JGetInt(body, "heath_status_interval");
LOG_ERR("RECEIVED VALUE: %d\n",test_value); // 

JDelete(body);  // I figured this might be needed to free mem from body?
NoteDeleteResponse(rsp);

And here is the resulting console output:

[INFO] {“req”:“env.get”,“crc”:“0016:A0700FD4”}
[INFO] {“time”:1727187854,“body”:{“_tri_mins”:“1440”,“filter_rssi_min”:“-128”,“heath_status_interval”:“30”,“uploader_sync_interval”:“3”,“_fw”:“gr3_Jun21$20240621202057.binpack”,“_fw_retry”:“1719003592”,“_fwc”:“notecard-6.2.3.16585$20240517112300.bin”,“_fwc_retry”:“1719001785”,“_sn”:“scott_desk”,“_tags”:“300 Dr\r\nTX\r\n75602\r\nDevice ID 6789546”,“reading_interval”:“600”}}
[00:15:00.329,803] config_log: RECEIVED VALUE: 0

Using a debugger, I found that where things are breaking down is when the JGetInt routine checks to see if the “item” is a number. In line 235 of n_cjson_helpers.c, this expression evaluates to true:

!JIsNumber(item)

As a result, JGetInt returns zero.

Any idea what is going on here? Is the 30 coming from env variables actually a string and JGetInt doesn’t automatically parse it to int?

Scott

Ah! Yeah sorry I missed that in my first response. All environment variables are stored as strings, and JGetInt doesn’t convert because it’s dumb and just sees a string ("30") in the response.

To get the int out you’ll have to do the conversion yourself with something like this:

int value = atoi(JGetString(body, "heath_status_interval"));

TJ

1 Like

Great! Thanks for the help here, TJ. Looks like it’s working now.

Scott

1 Like