Create dynamic Json


I am trying to create a dynamic Json, the only issue is that I don’t know how to add the multiple pairs.

My Json format is like

“key1” : “value1”,
“key2” : “value2”,

“key3”: [ {key4: “value4”, key5:“value5”, key6: “value6”} , {key4: “value7”, key5:“value8”, key6: “value9”}, ] etc depending on how many values I have to add to key 3.

I tried to add these values with Invoke method but I’ve got the following error :

Can not add property “key3” to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.

Any ideas ?

Thanks in advance !

Easiest way to build JSON is with Invoke Code.

Here is an example:

Dim jMain As JObject = New JObject

'add header section

'add default section
Dim jDefault As JObject = New JObject

'build examples, required, properties
Dim jExample As JObject = New JObject
Dim jRequired As JArray = New JArray
Dim jProperties As JObject = New JObject
For Each CurrentRow As DataRow In DT_Properties.AsEnumerable
	'add to required section
	If CBool(CurrentRow("Required").ToString) Then
	End If
	'build property
	Dim jProperty As JObject = New JObject
	Dim jPropertyExamples As JArray = New JArray
	jProperty.Add("$id","#/properties/" + CurrentRow("Field_Name").ToString)
	jProperty.Add("title","The " + CurrentRow("Field_Name").ToString + " schema")
	Select Case CurrentRow("Datatype").ToString
	Case "string"
		If Not (CurrentRow("String_Format").ToString = "none" Or String.IsNullOrEmpty(CurrentRow("String_Format").ToString)) Then
		End If
	Case "integer"
		If Not String.IsNullOrEmpty(CurrentRow("Example_Value").ToString) Then
		End If
	End Select
Dim jExamples As JArray = New JArray
schemaJSONStr = jMain.ToString

In some cases we can easy achieve it by using a Dictionary(Of String, Object) and then get it converted to a JObject / JSON

Are you looking for constructing the JObject by scratch?

Sorry. I saved the post before finishing it. Now I’ve added all the details. And yes, I am building the JSON from scratch, but my only issue is when I try to add the array formed with 3 values to “key3”, because I can have multiple arrays that I would need to add.

You have to build the array first by adding all the JObjects to it, then add it just once.

we do feel that with your updated sample the question is different / changed
We gave you a few options, but also asked if you a looking for a by scratch creation

Once we do know your answer and/or preferences on the approach, we will go ahead with a next step. Thanks for support

Everything works fine until I have to add to “key3” the array formed with 3 values. For example, one day I can have 5 pairs of 3 values to add to “key3”, the next day I need to add 15 pairs.

And it doesn’t work to add them with invoke method. Or maybe I’m doing something wrong.

Yes, I am building the JSON from scratch, but my only issue is when I try to add the array formed with 3 values to “key3”, because I can have multiple arrays that I would need to add.

For example, one day I can have 5 pairs of 3 values to add to “key3”, the next day I need to add 15 pairs.

And it doesn’t work to add them with invoke method. Or maybe I’m doing something wrong

show us the final sample, which should be the expected result. Please use the </> Format button fro the editor or share it with us as text file.

From above sample, we cannot fully derrive on what you try.

“key1”: “sample string”,
“key2”: “sample string”,
“key3”: [
“key4”: “sample string 1”,
“key5”: “sample string 2”,
“key5”: “sample string 3”
“key4”: “sample string 1”,
“key5”: “sample string 2”,
“key6”: “sample string 3”

You have to add all the arrays to key3 before adding key3 as a JObject.

Could it be the case that a JObject Add (adding a Property to JSON) and an JArray Add is mismatching used?

Sample 1:
Part result:

Series continuation:

with result:

Series continuation:

variation on the JObject construction and added as second Array item

Series continuation - Adding to the first array item an additional Property:


myJObject = New JObject()
myJObject.Add(New JProperty("key1","samplestring"))
myJObject.Add(New JProperty("key3",New JArray()))
myJObject("key3").Value(Of JArray).Add(New JObject({New JProperty("key4","V1"),New JProperty("key5","V2"),New JProperty("key6","V3")}))

Dim otherJO As JObject = New JObject()
otherJO.Add("key4", "V1")
otherJO.Add("key5", "V2")
otherJO.Add("key6", "V3")
myJObject("key3").Value(Of JArray).Add(otherJO)

myJObject("key3").Value(Of JArray).First().Value(Of JObject).Add("key7","V4")

So we show cased on how:

  • add Properties on a JObject
  • add a JArray Value and populate it with JObjects
  • access a dedicated item from the JArray and also its JObject and edit it e.g. adding an additional Property

we assume that this was a typo as Keys have to be unique

Thank you very much ! But how cand I adapt this assuming that I have a for each and I need to add one more property each time ?

When “For each” ends I need to have the main json and 100 properties added during the iterations.

We introduced you to the main building blocks and demonstrated how in general we can construct theJSON.

Now you can use and advance it to your needed and dynamized logic

we would assume, that for this the already presented approaches / Statements / API Calls will serve

Simply execute below code using “Invoke code” activity enclosed in the FOR EACH


1 Like

Or without “Invoje Code” just with assign statements:

For Each <something>
  newItem (of JObject) = New JObject({New JProperty("key4","V1"),New JProperty("key5","V2"),New JProperty("key6","V3")})

  myJObject("key3") = JArray.FromObject(myJObject("key3").Concat({newItem}).toArray)
End For Each
1 Like

Thank you guys. I finally managed to solve the issue.

So before the “for each” I invoked code with

myJObject = New JObject()
myJObject.Add(New JProperty(“key1”,“samplestring”))

And then in “for each” I invoked code again with

myJObject(“key3”).Value(Of JArray).Add(New JObject({New JProperty(“key4”,“V1”),New JProperty(“key5”,“V2”),New JProperty(“key6”,“V3”)}))

The only mention is that the argument myJObject in “for each” needs to be IN/OUT.

Thanks again for the help !

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.