Best way to create multiple JSON Elements?

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...

2 Likes