Create dynamic Json

Hello,

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
jMain.Add("$schema","http://json-schema.org/draft-07/schema")
jMain.Add("$id","http://example.com/example.json")
jMain.Add("type","object")
jMain.Add("title",schemaTitle)
jMain.Add("description",schemaDescription)

'add default section
Dim jDefault As JObject = New JObject
jMain.Add("default",jDefault)

'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
		jRequired.Add(CurrentRow("Field_Name").ToString)
	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("type",CurrentRow("Datatype").ToString)
	jProperty.Add("title","The " + CurrentRow("Field_Name").ToString + " schema")
	jProperty.Add("description",CurrentRow("Field_Name").ToString)
	Select Case CurrentRow("Datatype").ToString
	Case "string"
		JExample.Add(CurrentRow("Field_Name").ToString,CurrentRow("Example_Value").ToString)
		jPropertyExamples.Add(CurrentRow("Example_Value").ToString)
		If Not (CurrentRow("String_Format").ToString = "none" Or String.IsNullOrEmpty(CurrentRow("String_Format").ToString)) Then
			jProperty.Add("format",CurrentRow("String_Format").ToString)
		End If
	Case "integer"
		If Not String.IsNullOrEmpty(CurrentRow("Example_Value").ToString) Then
			JExample.Add(CurrentRow("Field_Name").ToString,CInt(CurrentRow("Example_Value").ToString))
			jPropertyExamples.Add(CInt(CurrentRow("Example_Value").ToString))
		End If
	End Select
	jProperties.Add(CurrentRow("Field_Name").ToString,jProperty)
	
	
	jProperty.Add("examples",jPropertyExamples)
Next
Dim jExamples As JArray = New JArray
jExamples.Add(jExample)
jMain.Add("examples",jExamples)
jMain.Add("required",jRequired)
jMain.Add("properties",jProperties)
jMain.Add("additionalProperties",additionalProperties)
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:
grafik
grafik
Part result:
grafik

Series continuation:


with result:
grafik

Series continuation:


variation on the JObject construction and added as second Array item
grafik

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


grafik

myJObject = New JObject()
myJObject.Add(New JProperty("key1","samplestring"))
myJObject.Add("key2","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

However:
grafik
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

Cheers

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”))
myJObject.Add(“key2”,“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.