# string(JSON ... SET ...) example

**URL:** https://discourse.cmake.org/t/string-json-set-example/2746
**Category:** Code
**Created:** [February 12, 2021, 12:24am UTC](https://discourse.cmake.org/t/string-json-set-example/2746 "2021-02-12T00:24:27Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [February 12, 2021, 12:24am UTC](https://discourse.cmake.org/t/string-json-set-example/2746/1 "2021-02-12T00:24:27Z")

</div>

I’ve found the `string(JSON ... GET)` to be a great way to separate project data (urls, md5s, git tags) from code by reading in a json file with those parameters. Especially useful for projects with lots of FetchContent/ExternalProject.

A general observation: I’ve been thinking about applications for `string(JSON ... SET)` to store build metadata without using cmake-server or trace mode. Say for reproducibility and storing of a few key parameters. It looks like this can act like a mappable (dictionary) almost. I came up with this toy example of incrementally building up JSON string as one might with a dictionary in Python.

```cmake
set(dat "{}")
set(fake "abc" "def" "xyz")

foreach(i RANGE 1)

  string(JSON dat SET ${dat} key${i} "{}")
  foreach(j RANGE 2)
    list(GET fake ${j} x)
    string(JSON dat SET ${dat} key${i} prp${j} \"${x}\")
  endforeach()

endforeach()

message(${dat})

```

results in:

```json
{
  "key0" :
  {
    "prp0" : "abc",
    "prp1" : "def",
    "prp2" : "xyz"
  },
  "key1" :
  {
    "prp0" : "abc",
    "prp1" : "def",
    "prp2" : "xyz"
  }
}

```
