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