# Initialize storage record value with an array

**URL:** <https://forum.heroiclabs.com/t/initialize-storage-record-value-with-an-array/103>\
**Category:** Local Setup\
**Created:** [July 29, 2019, 5:29pm UTC](https://forum.heroiclabs.com/t/initialize-storage-record-value-with-an-array/103 "2019-07-29T17:29:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oscargoldman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/oscargoldman/32/28_2.png) [@oscargoldman](https://forum.heroiclabs.com/u/oscargoldman)\
**Post date:** [July 29, 2019, 5:29pm UTC](https://forum.heroiclabs.com/t/initialize-storage-record-value-with-an-array/103/1 "2019-07-29T17:29:54Z")

</div>

Is there any way to create via a lua module, a storage record with a value that looks like this:  
`"{names:[]}"`  
It seems that from lua any empty table is always created as an empty object in json, and only becomes an json array when there’s more than one nested object.

---

<div class="post-metadata">

**Author:** ![oscargoldman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/oscargoldman/32/28_2.png) [@oscargoldman](https://forum.heroiclabs.com/u/oscargoldman)\
**Post date:** [July 29, 2019, 5:32pm UTC](https://forum.heroiclabs.com/t/initialize-storage-record-value-with-an-array/103/2 "2019-07-29T17:32:37Z")

</div>

that’s supposed to be a pair of square brackets, not a checkbox.

---

<div class="post-metadata">

**Author:** ![zyro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/zyro/32/14_2.png) [@zyro](https://forum.heroiclabs.com/u/zyro)\
**Post date:** [July 29, 2019, 7:33pm UTC](https://forum.heroiclabs.com/t/initialize-storage-record-value-with-an-array/103/3 "2019-07-29T19:33:12Z")

</div>

Lua doesn’t strictly differentiate between ‘objects’ and ‘arrays’, the `table` type takes care of both so by default the JSON encoder can’t be sure if you intended to encode an empty table object or table array.

If you really need to strictly control it the easiest way is string substitution for the empty case on specific fields:

```lua
local json = "{\"names\":[],\"no_change\":[]}"
json = string.gsub(json, "\"names\":%[%]", "\"names\":{}")
print(json)

```

Will print:

```auto
{"names":{},"no_change":[]}

```

---

<div class="post-metadata">

**Author:** ![novabyte](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/novabyte/32/1324_2.png) [@novabyte](https://forum.heroiclabs.com/u/novabyte)\
**Post date:** [July 29, 2019, 7:51pm UTC](https://forum.heroiclabs.com/t/initialize-storage-record-value-with-an-array/103/4 "2019-07-29T19:51:23Z")

</div>

@oscargoldman This is one of the limitations with JSON and Lua. Lua only has one type to represent both arrays and map types. It uses an array-indexed table or a keyed table.

i.e.

```lua
local array = {"a", "b", "c"}
local map = {
  {"key1", "a"},
  {"key2", "b"}
}

```

Is it necessary for you to store empty arrays at all in your JSON structure?

---

<div class="post-metadata">

**Author:** ![oscargoldman](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/oscargoldman/32/28_2.png) [@oscargoldman](https://forum.heroiclabs.com/u/oscargoldman)\
**Post date:** [July 30, 2019, 5:35pm UTC](https://forum.heroiclabs.com/t/initialize-storage-record-value-with-an-array/103/5 "2019-07-30T17:35:08Z")

</div>

it was part of initializing required records for a new user, I was running into some serialization issues when reading the records back but we can work around it.
