# Note-c how to iterate over keys of an object?

**URL:** <https://discuss.blues.com/t/note-c-how-to-iterate-over-keys-of-an-object/2887>\
**Category:** Tools & SDKs\
**Created:** [February 28, 2025, 5:24pm UTC](https://discuss.blues.com/t/note-c-how-to-iterate-over-keys-of-an-object/2887 "2025-02-28T17:24:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![devElert](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@devElert](https://discuss.blues.com/u/devElert)\
**Post date:** [February 28, 2025, 5:24pm UTC](https://discuss.blues.com/t/note-c-how-to-iterate-over-keys-of-an-object/2887/1 "2025-02-28T17:24:04Z")

</div>

Are there any examples of how to iterate over keys and values of a JSON object?

I have tried this:

```auto
    // iterate over the keys
    J* key;
    JObjectForEach(name, cmd.params) {
        const char* keyname = JGetStringValue(key);
        J* value = JGetObjectItem(cmd.params, keyname);
        ...
    }

```

However, this doesn’t work, since `const char* key = JGetStringValue(name);` returns `nullptr`. This is because JGetStringValue is expecting (as you might guess from the name) a string value.

Internally, the `J` struct has a `stringvalue` pointer for true string values, and `string` for key names.

I could just use `key->string` to get the key name, but this is going behind the scenes. Is there an officially supported way to do this?

---

<div class="post-metadata">

**Author:** ![RobLauer](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/roblauer/32/1567_2.png) [@RobLauer](https://discuss.blues.com/u/RobLauer)\
**Post date:** [March 2, 2025, 4:17pm UTC](https://discuss.blues.com/t/note-c-how-to-iterate-over-keys-of-an-object/2887/2 "2025-03-02T16:17:29Z")

</div>

Hi @devElert,

You should be able to iterate over your JSON with this:

```auto
J *field = NULL;
JObjectForEach(field, object) {
    char *value = JStringValue(field);
    const char *key = JGetItemName(field);
}

```

---

<div class="post-metadata">

**Author:** ![devElert](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@devElert](https://discuss.blues.com/u/devElert)\
**Post date:** [March 7, 2025, 4:26pm UTC](https://discuss.blues.com/t/note-c-how-to-iterate-over-keys-of-an-object/2887/3 "2025-03-07T16:26:06Z")

</div>

Awesome, thanks! That’s perfect.
