# Can JSONitem store a Xojo array?

**URL:** <https://forum.xojo.com/t/can-jsonitem-store-a-xojo-array/25319>\
**Category:** General\
**Created:** [July 5, 2015, 6:14pm UTC](https://forum.xojo.com/t/can-jsonitem-store-a-xojo-array/25319 "2015-07-05T18:14:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Huber](https://forum.xojo.com/user_avatar/forum.xojo.com/joe_huber/32/475_2.png) [@Joe\_Huber](https://forum.xojo.com/u/Joe_Huber)\
**Post date:** [July 5, 2015, 6:14pm UTC](https://forum.xojo.com/t/can-jsonitem-store-a-xojo-array/25319/1 "2015-07-05T18:14:35Z")

</div>

Can a JSONitem really store a Xojo array?

The Xojo doc for JSONitem says:

[quote]JSON Objects can contain named data a collection of name-value pairs (like a Dictionary)…  
Values can be any of the following types: Strings, numbers, JSONItems, arrays, Booleans, or Nil[/quote]

But whenever I put an array into a JSONitem the subsequent JSONitem.toString gives me a Type Mismatch Exception

Is this a bug in Xojo, the docs or my head?

(I understand the workaround is to convert my array into an array style JSONitem, but the docs say arrays are supported directly… are they really?)

Thanks, Joe

This slightly modified example from the Xojo docs shows the issue:

[code]dim Person as new JSONItem  
//This object is manipulated like a dictionary  
Person.Value(“Name”) = “John Doe”  
Person.Value(“Age”) = 32  
Person.Value(“Married”) = True

//Trying to store an actual Xojo Array causes ToString to throw a Type Mismatch Exception below.  
Person.Value(“KidsArray”) = Array(“John Jr”, “Jamie”, “Jonah”)

//Convert to a JSON String  
dim s as String = Person.toString() //Type Mismatch Exception occurs here if the JSONitem contains a string array[/code]

---

<div class="post-metadata">

**Author:** ![Peter\_Fargo](https://forum.xojo.com/user_avatar/forum.xojo.com/peter_fargo/32/977_2.png) [@Peter\_Fargo](https://forum.xojo.com/u/Peter_Fargo)\
**Post date:** [July 5, 2015, 7:18pm UTC](https://forum.xojo.com/t/can-jsonitem-store-a-xojo-array/25319/2 "2015-07-05T19:18:04Z")

</div>

When you use YourJSONItem.Value(x) = y you are creating a single value. When you use YouJSONItem.Append y you are creating an array.

Create a JSONItem and then loop through your array and append each array member to it.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [July 5, 2015, 7:58pm UTC](https://forum.xojo.com/t/can-jsonitem-store-a-xojo-array/25319/3 "2015-07-05T19:58:49Z")

</div>

I think the docs are misleading and what it means is that you can spend another JSONItem that is acting as an array.

If to use the new framework, you can append an array directly.

---

<div class="post-metadata">

**Author:** ![Yosef\_Coelho](https://forum.xojo.com/letter_avatar_proxy/v4/letter/y/cc9497/32.png) [@Yosef\_Coelho](https://forum.xojo.com/u/Yosef_Coelho)\
**Post date:** [July 12, 2015, 9:53am UTC](https://forum.xojo.com/t/can-jsonitem-store-a-xojo-array/25319/4 "2015-07-12T09:53:34Z")

</div>

I think Joe meant this:

```auto
  
  Dim jsString As String
  Dim myArray() As String
  Dim myJSONItem As New JSONItem
  
  myArray = Array("james", "jack", "john")
  
  myJSONItem.Value("somestring") = myArray
  
  // Then
  
  jsString = myJSONItem.ToString
  
  // jsString should be: {"somestring":["james", "jack", "john"]}, but it's not, type mismatch error.
  
  // Instead, you can use JSONItems as arrays:
  
  Dim myJSONItemArray As New JSONItem
  
  myJSONItemArray.Append("Jenny")
  myJSONItemArray.Append("Beth")
  myJSONItemArray.Append("Can't be bothered")
  
  // Then it will work:
  
  myJSONItem.Value("somestring") = myJSONItemArray
  
  jsString = myJSONItem.ToString 
  
  // jsString IS: {"some string":["Jenny", "Beth", "Can't be bothered"]}

```

So, yes, JSONItem can’t be assigned a XOJO array. You can extend it though and handle the array etc.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:49pm UTC](https://forum.xojo.com/t/can-jsonitem-store-a-xojo-array/25319/5 "2020-10-29T17:49:58Z")

</div>


