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

**URL:** <https://discuss.blues.com/t/reading-data-from-a-notecard-response-with-c-c-api/2332>\
**Category:** Notecard API\
**Created:** [September 23, 2024, 5:57pm UTC](https://discuss.blues.com/t/reading-data-from-a-notecard-response-with-c-c-api/2332 "2024-09-23T17:57:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scase](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@scase](https://discuss.blues.com/u/scase)\
**Post date:** [September 23, 2024, 5:57pm UTC](https://discuss.blues.com/t/reading-data-from-a-notecard-response-with-c-c-api/2332/1 "2024-09-23T17:57:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tjvantoll](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/tjvantoll/32/231_2.png) [@tjvantoll](https://discuss.blues.com/u/tjvantoll)\
**Post date:** [September 23, 2024, 8:42pm UTC](https://discuss.blues.com/t/reading-data-from-a-notecard-response-with-c-c-api/2332/2 "2024-09-23T20:42:03Z")

</div>

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:

```c
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](https://dev.blues.io/tools-and-sdks/firmware-libraries/arduino-library/#parsing-json-objects)

TJ

---

<div class="post-metadata">

**Author:** ![scase](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@scase](https://discuss.blues.com/u/scase)\
**Post date:** [September 23, 2024, 8:44pm UTC](https://discuss.blues.com/t/reading-data-from-a-notecard-response-with-c-c-api/2332/3 "2024-09-23T20:44:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tjvantoll](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/tjvantoll/32/231_2.png) [@tjvantoll](https://discuss.blues.com/u/tjvantoll)\
**Post date:** [September 24, 2024, 1:14pm UTC](https://discuss.blues.com/t/reading-data-from-a-notecard-response-with-c-c-api/2332/4 "2024-09-24T13:14:08Z")

</div>

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](https://blues.github.io/note-c/getting_started.html).

TJ

---

<div class="post-metadata">

**Author:** ![scase](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@scase](https://discuss.blues.com/u/scase)\
**Post date:** [September 24, 2024, 4:32pm UTC](https://discuss.blues.com/t/reading-data-from-a-notecard-response-with-c-c-api/2332/5 "2024-09-24T16:32:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tjvantoll](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/tjvantoll/32/231_2.png) [@tjvantoll](https://discuss.blues.com/u/tjvantoll)\
**Post date:** [September 24, 2024, 5:08pm UTC](https://discuss.blues.com/t/reading-data-from-a-notecard-response-with-c-c-api/2332/6 "2024-09-24T17:08:42Z")

</div>

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:

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

```

TJ

---

<div class="post-metadata">

**Author:** ![scase](https://avatars.discourse-cdn.com/v4/letter/s/5daacb/32.png) [@scase](https://discuss.blues.com/u/scase)\
**Post date:** [September 25, 2024, 3:14pm UTC](https://discuss.blues.com/t/reading-data-from-a-notecard-response-with-c-c-api/2332/7 "2024-09-25T15:14:37Z")

</div>

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

Scott
