# How to INSERT/PREPEND an element into a JSON array using string(JSON)?

**URL:** https://discourse.cmake.org/t/how-to-insert-prepend-an-element-into-a-json-array-using-string-json/8410
**Category:** Code
**Created:** [June 26, 2023, 4:53pm UTC](https://discourse.cmake.org/t/how-to-insert-prepend-an-element-into-a-json-array-using-string-json/8410 "2023-06-26T16:53:25Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hwhsu1231](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hwhsu1231/32/5358_2.png) [@hwhsu1231](https://discourse.cmake.org/u/hwhsu1231)
#### Post date: [June 26, 2023, 4:53pm UTC](https://discourse.cmake.org/t/how-to-insert-prepend-an-element-into-a-json-array-using-string-json/8410/1 "2023-06-26T16:53:25Z")

</div>

Suppose that there is a JSON file (called `array.json`) with the following structure:

```json
{
  "array":
  [
    {
      "name" : "element1",
      "property" : "value1"
    },
    {
      "name" : "element2",
      "property" : "value2"
    }
  ]
}

```

I wonder how to use [string(JSON)](https://cmake.org/cmake/help/v3.27/command/string.html#json) command to:

- INSERT a new element (`element3`) into the array between `element1` and `element2`?
- PREPEND a new element (`element3`) into the array?

BTW, I already know how to APPEND a new element (`element3`) into the array. The followings are my example codes:

> **Click to expand "json.cmake"**
>
> ```cmake
> file(READ "${CMAKE_CURRENT_LIST_DIR}/array.json" jsonCnt)
> 
> message("---------- array.json (before) ----------")
> message("${jsonCnt}")
> 
> string(JSON arrayCnt GET "${jsonCnt}" "array")
> 
> message("---------- array (before) ----------")
> message("${arrayCnt}")
> 
> # Create the 3rd element
> set(newElemCnt "{}")
> string(JSON newElemCnt SET "${newElemCnt}" "name" "\"element3\"")
> string(JSON newElemCnt SET "${newElemCnt}" "property" "\"value3\"")
> 
> # Append the 3rd element into the "array"
> string(JSON arrayLen LENGTH "${arrayCnt}")
> string(JSON arrayCnt SET "${arrayCnt}" ${arrayLen} "${newElemCnt}")
> 
> message("---------- array (after) ----------")
> message("${arrayCnt}")
> 
> string(JSON jsonCnt SET "${jsonCnt}" "array" "${arrayCnt}")
> 
> message("---------- array.json (after) ----------")
> message("${jsonCnt}")
> 
> ```

> **Click to expand "array.json"**
>
> ```json
> {
> "array":
> [
> {
> "name" : "element1",
> "property" : "value1"
> },
> {
> "name" : "element2",
> "property" : "value2"
> }
> ]
> }
> 
> ```

After running the command `cmake -P json.cmake` in CLI, the output is:

> **Click to expand**
>
> ```auto
> ---------- array.json (before) ----------
> {
> "array":
> [
> {
> "name" : "element1",
> "property" : "value1"
> },
> {
> "name" : "element2",
> "property" : "value2"
> }
> ]
> }
> ---------- array (before) ----------
> [
> {
> "name" : "element1",
> "property" : "value1"
> },
> {
> "name" : "element2",
> "property" : "value2"
> }
> ]
> ---------- array (after) ----------
> [
> {
> "name" : "element1",
> "property" : "value1"
> },
> {
> "name" : "element2",
> "property" : "value2"
> },
> {
> "name" : "element3",
> "property" : "value3"
> }
> ]
> ---------- array.json (after) ----------
> {
> "array" : 
> [
> {
> "name" : "element1",
> "property" : "value1"
> },
> {
> "name" : "element2",
> "property" : "value2"
> },
> {
> "name" : "element3",
> "property" : "value3"
> }
> ]
> }
> 
> ```

However, I just cannot figure out how to INSERT/PREPEND an element into the array. Is it possible to use the currently available subcommands (SET, GET, LENGTH, MEMBER…etc) of string(JSON) to implement these two operations?

If not, I would like to request the support for APPEND/INSERT/PREPEND subcommands of string(JSON).

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [June 26, 2023, 5:01pm UTC](https://discourse.cmake.org/t/how-to-insert-prepend-an-element-into-a-json-array-using-string-json/8410/2 "2023-06-26T17:01:03Z")

</div>

> [@hwhsu1231](#):
>
> cc: @brad.king @craig.scott

Please don’t Cc maintainers on every question.

---

<div class="post-metadata">

### Author: ![hwhsu1231](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hwhsu1231/32/5358_2.png) [@hwhsu1231](https://discourse.cmake.org/u/hwhsu1231)
#### Post date: [June 26, 2023, 5:03pm UTC](https://discourse.cmake.org/t/how-to-insert-prepend-an-element-into-a-json-array-using-string-json/8410/3 "2023-06-26T17:03:00Z")

</div>

Got it. Sorry about that.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [July 1, 2023, 8:53pm UTC](https://discourse.cmake.org/t/how-to-insert-prepend-an-element-into-a-json-array-using-string-json/8410/4 "2023-07-01T20:53:55Z")

</div>

These do indeed seem to be missing operations. I think a [feature request](https://gitlab.kitware.com/cmake/cmake/-/issues) makes sense at least.
