# Best way to create multiple JSON Elements?

**URL:** <https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027>\
**Category:** Questions\
**Tags:** array\
**Created:** [June 3, 2020, 11:37am UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027 "2020-06-03T11:37:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon45965781](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon45965781](https://the.fmsoup.org/u/anon45965781)\
**Post date:** [June 3, 2020, 11:37am UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/1 "2020-06-03T11:37:03Z")

</div>

Trying to create a JSON structure in FileMaker like this:

{  
"Cars" : "[VW, GM, Other]",  
"First\_Name" : "Alan",  
"Last\_Name" : "Jones"  
}  
{  
"Cars" : "[Ford, Lexus, BMW]",  
"First\_Name" : "John",  
"Last\_Name" : "Smith"  
}

I've tried two approaches, but both just give me the last JSON element.

**Approach 1: (two set variables -- second one overwrites the $$JSON variable - expected result with this approach)**

Set Variable [$$JSON ; Value: JSONSetElement ("{}" ; [ "First\_Name" ; "John" ; 0] ; ["Last\_Name" ; "Smith" ; 0] ; ["Cars" ; "[Ford, Lexus, BMW]" ;0] ) ]  
Set Variable [$$JSON ; Value: JSONSetElement ("{}" ; [ "First\_Name" ; "Alan" ; 0] ; ["Last\_Name" ; "Jones" ; 0] ; ["Cars" ; "[VW, GM, Other]" ;0] ) ]

# 

Set Field [Untitled::json ; JSONFormatElements ( $$JSON )]

* * *

**Approach 2: - build in a single Set Variable**

Set Variable [$$JSON ; Value: JSONSetElement ("{}" ; [ "First\_Name" ; "John" ; 0] ; ["Last\_Name" ; "Smith" ; 0] ; ["Cars" ; "[Ford, Lexus, BMW]" ;0] ; ["First\_Name" ; "Alan" ; 1] ; ["Last\_Name" ; "Jones" ; 1] ; ["Cars" ; "[VW, GM, Other]" ;1] ) ]

Set Field [Untitled::json ; JSONFormatElements ( $$JSON )]

===

Result:  
{  
"Cars" : "[VW, GM, Other]",  
"First\_Name" : "Alan",  
"Last\_Name" : "Jones"  
}

Suggestions?

Thanks,

---

<div class="post-metadata">

**Author:** ![JamesG](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@JamesG](https://the.fmsoup.org/u/JamesG)\
**Post date:** [June 3, 2020, 12:30pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/2 "2020-06-03T12:30:32Z")

</div>

Looks like you need to make each Cars, First\_Name and Last\_Name objects entries in an array, or define them with unique object names:

**Uniques Names:**

```
JSONSetElement ( "{}" 

; ["customer1.Cars" ; "[VW,GM,Other]" ; JSONString ]
; ["customer1.First_Name" ; "Alan" ; JSONString]
; ["customer1.Last_Name" ; "Jones" ; JSONString]

; ["customer2.Cars" ; "[Ford,Lexus,BMW]" ; JSONString ]
; ["customer2.First_Name" ; "John" ; JSONString]
; ["customer2.Last_Name" ; "Smith" ; JSONString]

```

)

to give:

```
{
	"customer1" : 
	{
		"Cars" : "[VW,GM,Other]",
		"First_Name" : "Alan",
		"Last_Name" : "Jones"
	},
	"customer2" : 
	{
		"Cars" : "[Ford,Lexus,BMW]",
		"First_Name" : "John",
		"Last_Name" : "Smith"
	}
}

```

But the better approach would likely be to set them up as array entries, meaning you don't have manually label each element:

```
Let ( 

[

customer_object_1 = 
JSONSetElement ( "{}" 

; ["Cars" ; "[VW,GM,Other]" ; JSONString ]
; ["First_Name" ; "Alan" ; JSONString]
; ["Last_Name" ; "Jones" ; JSONString]
)

;customer_object_2 = 
JSONSetElement ( "{}" 
; ["Cars" ; "[Ford,Lexus,BMW]" ; JSONString ]
; ["First_Name" ; "John" ; JSONString]
; ["Last_Name" ; "Smith" ; JSONString]
)

; customer_array = 

JSONSetElement ( "{}" 

; ["Customers.[0]." ; customer_object_1 ; JSONObject ]
; ["Customers.[1]." ; customer_object_2 ; JSONObject ]

)

]

;

JSONFormatElements ( customer_array )

//Close Let
)

```

To give:

```
{
	"Customers" : 
	[
		{
			"Cars" : "[VW,GM,Other]",
			"First_Name" : "Alan",
			"Last_Name" : "Jones"
		},
		{
			"Cars" : "[Ford,Lexus,BMW]",
			"First_Name" : "John",
			"Last_Name" : "Smith"
		}
	]
}

```

In the latter example, the customer objects would normally be built up using a looping script (like you tried originally), with each object then inserted into the next available array index...

---

<div class="post-metadata">

**Author:** ![jormond](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@jormond](https://the.fmsoup.org/u/jormond)\
**Post date:** [June 3, 2020, 1:14pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/3 "2020-06-03T13:14:24Z")

</div>

My first question would be why do you need them separate like that? Obviously, it's not standard JSON.

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [June 3, 2020, 1:14pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/4 "2020-06-03T13:14:28Z")

</div>

> [@JamesG](#):
>
> In the latter example, the customer objects would normally be built up using a looping script (like you tried originally), with each object then inserted into the next available array index...

That's typically how these things are built. You loop over each customer and construct the json for that customer, then before moving on to the next customer you add the customer json object to the array.

---

<div class="post-metadata">

**Author:** ![anon45965781](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon45965781](https://the.fmsoup.org/u/anon45965781)\
**Post date:** [June 3, 2020, 1:52pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/5 "2020-06-03T13:52:47Z")

</div>

Sorry, my initial posting was not totally accurate, but your reply helped me a lot anyway! 🙂

My confusion trying to do JSON in FileMaker is what is hard coded and what, if anything, is object based. Being a Java guy, how FileMaker does JSON is very non-intuitive to me though the steps are mostly the same. I'm not saying FMP's JSON is bad or anything along those lines. Rather, FMP's JSON is very different than corresponding Java logic to create your corrected version of my example:

 ![json](https://canada1.discourse-cdn.com/flex030/uploads/fmsoup/original/1X/9b8229d3ba5c5f7aad73060a9b9a84493437b706.png)

I really appreciated your reply. I saved your example in the data viewer!

Thanks very much.

---

<div class="post-metadata">

**Author:** ![anon45965781](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon45965781](https://the.fmsoup.org/u/anon45965781)\
**Post date:** [June 3, 2020, 2:36pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/6 "2020-06-03T14:36:51Z")

</div>

Yep, I mis-posted my example...

@JamesG below did an awesome job with his reply clarifying my posting.

---

<div class="post-metadata">

**Author:** ![WimDecorte](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@WimDecorte](https://the.fmsoup.org/u/WimDecorte)\
**Post date:** [June 3, 2020, 3:42pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/7 "2020-06-03T15:42:37Z")

</div>

The last screenshot you posted shows the 'cars' element as an array, whereas your first post shows it as a string.  
If you want an array you'll have to change the example to make it so, but you get the concept of the JsonSetElement() by now I'm sure.

---

<div class="post-metadata">

**Author:** ![anon45965781](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@anon45965781](https://the.fmsoup.org/u/anon45965781)\
**Post date:** [June 3, 2020, 4:11pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/8 "2020-06-03T16:11:43Z")

</div>

Right.

Since I hadn't used them much, I was just messing around with the JSON functions in FMP and wasn't consistent with my posting.

The FMP JSON functions, once you understand their syntax, seem very straightforward.

Thanks for your reply.

---

<div class="post-metadata">

**Author:** ![BoloMan28](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/boloman28/32/1506_2.png) [@BoloMan28](https://the.fmsoup.org/u/BoloMan28)\
**Post date:** [July 15, 2021, 10:07pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/9 "2021-07-15T22:07:04Z")

</div>

Hello Wim,

I guess this is a bit of a silly question, but it's the only way to learn.  
What are the principal application differences between a JSON Array and the JSON Element.

I have been using JSON Elements to pass variables and find it to be a quantum leap over simple variable passing. But I can't wrap my head around the use of arrays.

TIA

---

<div class="post-metadata">

**Author:** ![bdbd](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bdbd/32/620_2.png) [@bdbd](https://the.fmsoup.org/u/bdbd)\
**Post date:** [July 15, 2021, 11:22pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/10 "2021-07-15T23:22:04Z")

</div>

Hello @BoloMan28,

Wim Decorte is no longer with The Soup… so don't expect an answer from him here any time soon.

A JSON element is a key-value pair. Order in the JSON result is not important. A JSON array is a list of identically-typed values. Creation order is preserved.

When would you use an array? Imagine you want to record a series of coordinates to define a shape. An array becomes a very useful construct in this case, where the first entry represents the first coordinate, the second entry represents the next coordinate… and so on. Adding a key and value type to each coordinate would become overly complex. How would you determine the key name for an undefined number of keys?

---

<div class="post-metadata">

**Author:** ![BoloMan28](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/boloman28/32/1506_2.png) [@BoloMan28](https://the.fmsoup.org/u/BoloMan28)\
**Post date:** [July 16, 2021, 12:19am UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/11 "2021-07-16T00:19:10Z")

</div>

Hi David,

Thanks for the update, my understanding is as you described. Creating an Element seems to be quite easy; I have a few custom functions that can assemble elements in just a few lines of code. However, Arrays seem to be much more involved to create. I realize that a few of the custom functions I use for Elements can apply to arrays as well. But I really love the certainty offered by the named pairs.

That said, the Array does seem better when moving large pieces of code where the sequence can be accommodated when being parsed out or is not of relevance, using the assembly order as is.  
Thanks again for taking the time.

---

<div class="post-metadata">

**Author:** ![Bobino](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bobino/32/194_2.png) [@Bobino](https://the.fmsoup.org/u/Bobino)\
**Post date:** [July 16, 2021, 2:35am UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/12 "2021-07-16T02:35:28Z")

</div>

> [@bdbd](#):
>
> A JSON array is a list of identically-typed values.

@bdbd I'm not sure if this is part of the JSON specification, unless you meant something else.

> An _array_ is an ordered collection of values. An array begins with [left bracket and ends with]right bracket. Values are separated by ,comma.  
> A _value_ can be a _string_ in double quotes, or a _number_ , or true or false or null, or an _object_ or an _array_ . These structures can be nested.

[![](https://www.json.org/img/array.png) ](https://www.json.org/img/array.png)

source: [JSON](https://www.json.org/json-en.html)

---

<div class="post-metadata">

**Author:** ![steve\_ssh](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/steve_ssh/32/160_2.png) [@steve\_ssh](https://the.fmsoup.org/u/steve_ssh)\
**Post date:** [July 16, 2021, 3:38am UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/13 "2021-07-16T03:38:20Z")

</div>

+1

I think @bdbd's explanation of the difference between Objects and Arrays excellent, but that same detail caught my eye. I don't believe it is a requirement for the entries of a JSON array to be of like type, though I recognize that many times in practice, such will be the case.

---

<div class="post-metadata">

**Author:** ![jwilling](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/jwilling/32/256_2.png) [@jwilling](https://the.fmsoup.org/u/jwilling)\
**Post date:** [July 16, 2021, 4:09am UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/14 "2021-07-16T04:09:28Z")

</div>

Some languages do require arrays to contain only a single type. JavaScript and JSON allow the array to contain a mixture of types.

I think the term for a collection of ordered unnamed elements of potentially mixed types, is 'tuple'. So when people ask about your arrays-as-params technique, I recommend repeatedly saying "pseudo-tuple" in a pretentious voice, and maybe also suggest that you only resorted to pseudo-tuples because FM doesn't support protobufs.

---

<div class="post-metadata">

**Author:** ![OliverBarrett](https://avatars.discourse-cdn.com/v4/letter/o/f19dbf/32.png) [@OliverBarrett](https://the.fmsoup.org/u/OliverBarrett)\
**Post date:** [July 16, 2021, 3:12pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/15 "2021-07-16T15:12:09Z")

</div>

Yep, Java, Python, and C# all allow multiple types in arrays. I just tested with a string, Boolean, integer, and double values.

---

<div class="post-metadata">

**Author:** ![bdbd](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/bdbd/32/620_2.png) [@bdbd](https://the.fmsoup.org/u/bdbd)\
**Post date:** [July 16, 2021, 9:40pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/16 "2021-07-16T21:40:32Z")

</div>

@Bobino,

Thanks for catching that one. There is no need for types to be identical in FileMaker. As @steve_ssh pointed out, they are typically of the same type. As @jwilling pointed out, some languages make this a requirement of arrays. Not so with JSON… though it is not a language per se.

---

<div class="post-metadata">

**Author:** ![steve\_ssh](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/steve_ssh/32/160_2.png) [@steve\_ssh](https://the.fmsoup.org/u/steve_ssh)\
**Post date:** [July 17, 2021, 2:21pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/17 "2021-07-17T14:21:26Z")

</div>

> [@OliverBarrett](#):
>
> Yep, Java, Python, and C# all allow multiple types in arrays. I just tested with a string, Boolean, integer, and double values.

Hi @OliverBarrett

When you tested this with Java, did you define the array to have a data type of Object? That is the only way I can think of where you'd be able to store multiple data types in a Java array. Otherwise, my experience is that they are strongly typed.

---

<div class="post-metadata">

**Author:** ![OliverBarrett](https://avatars.discourse-cdn.com/v4/letter/o/f19dbf/32.png) [@OliverBarrett](https://the.fmsoup.org/u/OliverBarrett)\
**Post date:** [July 17, 2021, 2:32pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/18 "2021-07-17T14:32:16Z")

</div>

Hey Steve,

Here's the (hard-coded) test example, which uses "org.json.simple" library.

Let me know your thoughts.

```auto
org.json.simple.JSONObject first = new org.json.simple.JSONObject();
       first.put("first.name", "Alan");
       first.put("last.name", "Jones");
       first.put("owner", true);
       first.put("years.old", 30);
       **JSONArray cars = new JSONArray();**
       cars.add(0, "VW");
       cars.add(1, "GM");
       cars.add(2, "Lexus");
       cars.add(3, true);
       cars.add(4, 42);
       cars.add(5, 2.71);

       first.put("cars", cars);
       finalJson.add(first);

       // add second item...

       org.json.simple.JSONObject second = new org.json.simple.JSONObject();
       second.put("first.name", "Fred");
       second.put("last.name", "Smith");
       second.put("owner", false);
       second.put("years.old", 25);
       **JSONArray cars2 = new JSONArray();**
       cars2.add(0, "Tesla");
       cars2.add(1, "Toyota");
       cars2.add(2, "Chevy");
       cars2.add(3, false);
       cars2.add(4, 500);
       cars2.add(5, 3.14);

       second.put("cars", cars2);
       finalJson.add(second);

```

.  
.  
.

**Finally, prints:**

```auto
[
  {
    "owner": true,
    "years.old": 30,
    "cars": [
      "VW",
      "GM",
      "Lexus",
      true,
      42,
      2.71
    ],
    "last.name": "Jones",
    "first.name": "Alan"
  },
  {
    "owner": false,
    "years.old": 25,
    "cars": [
      "Tesla",
      "Toyota",
      "Chevy",
      false,
      500,
      3.14
    ],
    "last.name": "Smith",
    "first.name": "Fred"
  }
]

```

---

<div class="post-metadata">

**Author:** ![steve\_ssh](https://yyz2.discourse-cdn.com/flex030/user_avatar/the.fmsoup.org/steve_ssh/32/160_2.png) [@steve\_ssh](https://the.fmsoup.org/u/steve_ssh)\
**Post date:** [July 17, 2021, 2:45pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/19 "2021-07-17T14:45:41Z")

</div>

Ah! Thanks Oliver! This makes sense, and I see the disconnect I was having.

Indeed, multiple types are being stored within an array structure, and the language is clearly Java, but the array is not the array implementation provided by the Java language -- it is from the org.json.simple library. That was what I did not figure out from earlier context. Thanks so much for clarifying it for me.

As an added personal bonus: It has been a long time since I have looked at any Java code, and it used to be such a fond part of my life -- makes me nostalgic!

---

<div class="post-metadata">

**Author:** ![OliverBarrett](https://avatars.discourse-cdn.com/v4/letter/o/f19dbf/32.png) [@OliverBarrett](https://the.fmsoup.org/u/OliverBarrett)\
**Post date:** [July 17, 2021, 2:53pm UTC](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027/20 "2021-07-17T14:53:54Z")

</div>

Cool. Yes, the JSON library abstracts the data type issues so its "array" is not a Java array like you said also.

In Java, you could use an ArrayList() to also store any kind of object in an "array" type structure. An ArrayList is "like" an array, but has all the nice methods already implemented.

Typically, though (don't know how long it's been since you've been away from Java), you don't want multiple types in an ArrayList as that would be a super pain to interate over. Instead, you would use a construct (using Generics) that enforces a single Type (could be complex type like "Employee" or any type with any nested complexity) for the entire ArrayList.

**Example:**

```auto
List<Employee> emp = new ArrayList<>();
(you can only store Employee objects in this list - enforced).

The above instantiation also uses polymorphism to use the parent List Interface.
(Axiom obeyed: "Program to Interface, not to implementation", Gang-of-Four (aka, GoF) Design Patterns Book)

```

--

Thanks very much for your question and for your follow up!

Oliver

[Next page](https://the.fmsoup.org/t/best-way-to-create-multiple-json-elements/1027.md?page=2)
