Any json experts

I am creating JSON data from a string that I build like this

jString =  "{'mod':1,'val':[" + EndofLine  

for i as integer = 0 to 3 
  jString = jString +"{"   
  
  if ndx = 0 then ' tabpanel 0                                                        
    jString = jString + "'hx':" + wnd1_1.tHtgMx(i).Text +"," _                     
    + "'hn':" + wnd1_1.tHtgMn(i).Text +"," _                        
    + "'hd':" + wnd1_1.tHtgDif(i).Text +"," _                       
    + "'cx':" + wnd1_1.tClgMx(i).Text +"," _      
    + "'cn':" + wnd1_1.tClgMn(i).Text +"," _                        
    + "'cd':" + wnd1_1.tClgDif(i).Text +"," _                  
    + "'al':" + if(wnd1_1.talm(i).Value = True,"1","0") +"," _
    + "'st':" + if(wnd1_1.tst(i).Value = True,"1","0") +"," _
    + "'hw':" + if(wnd1_1.thw(i).Value = True,"1","0") +"," _ 
    + "'de':" + wnd1_1.tde(i).SelectedRowIndex.ToString
  else 'tabpanel 1
    jString = jString + "'hx':" + wnd2_1.tHtgMx(i).Text +"," _                        
    + "'hn':" + wnd2_1.tHtgMn(i).Text +"," _                        
    + "'hd':" + wnd2_1.tHtgDif(i).Text +"," _                       
    + "'cx':" + wnd2_1.tClgMx(i).Text +"," _      
    + "'cn':" + wnd2_1.tClgMn(i).Text +"," _                        
    + "'cd':" + wnd2_1.tClgDif(i).Text +"," _                  
    + "'al':" + if(wnd1_1.talm(i).Value = True,"1","0") +"," _
    + "'st':" + if(wnd1_1.tst(i).Value = True,"1","0") +"," _
    + "'hw':" + if(wnd1_1.thw(i).Value = True,"1","0") +"," _ 
    + "'de':" + wnd2_1.tde(i).SelectedRowIndex.ToString
  end if
if i < 3 then
  jString = jString + "},"+ EndofLine
else
  jString = jString + "}"+ EndofLine
end if
next

Which produces this JSON data.

{'mod':1,'val':[
{'hx':75,'hn':60,'hd':10,'cx':0,'cn':0,'cd':0,'al':0,'st':1,'hw':0,'de':1},
{'hx':60,'hn':50,'hd':5,'cx':0,'cn':0,'cd':0,'al':0,'st':0,'hw':0,'de':4},
{'hx':160,'hn':110,'hd':20,'cx':0,'cn':0,'cd':0,'al':0,'st':0,'hw':0,'de':3},
{'hx':0,'hn':0,'hd':0,'cx':0,'cn':0,'cd':0,'al':0,'st':0,'hw':0,'de':0}
]}

I send this JSON to a micro controller via s TCPSocket control.
I would like to use the xojo built in JSON functions to build the JSON data.
It seems that the code would be easier to read and maintain this way.
I can’t seem to figure out how to get the data in the format above.

Any help will be appreciated, thanks.

ChatGPT (3.5) :
Certainly! You can use Xojo’s built-in JSONItem class to simplify the creation of your JSON string. Here’s the modified code using JSONItem:

Dim json As New JSONItem
json.Value("mod") = 1
json.Value("val") = New JSONItem

For i As Integer = 0 To 3
  Dim item As New JSONItem
  
  If ndx = 0 Then ' tabpanel 0
    item.Value("hx") = wnd1_1.tHtgMx(i).Text
    item.Value("hn") = wnd1_1.tHtgMn(i).Text
    item.Value("hd") = wnd1_1.tHtgDif(i).Text
    item.Value("cx") = wnd1_1.tClgMx(i).Text
    item.Value("cn") = wnd1_1.tClgMn(i).Text
    item.Value("cd") = wnd1_1.tClgDif(i).Text
    item.Value("al") = If(wnd1_1.talm(i).Value, 1, 0)
    item.Value("st") = If(wnd1_1.tst(i).Value, 1, 0)
    item.Value("hw") = If(wnd1_1.thw(i).Value, 1, 0)
    item.Value("de") = wnd1_1.tde(i).SelectedRowIndex.ToString
  Else ' tabpanel 1
    item.Value("hx") = wnd2_1.tHtgMx(i).Text
    item.Value("hn") = wnd2_1.tHtgMn(i).Text
    item.Value("hd") = wnd2_1.tHtgDif(i).Text
    item.Value("cx") = wnd2_1.tClgMx(i).Text
    item.Value("cn") = wnd2_1.tClgMn(i).Text
    item.Value("cd") = wnd2_1.tClgDif(i).Text
    item.Value("al") = If(wnd1_1.talm(i).Value, 1, 0)
    item.Value("st") = If(wnd1_1.tst(i).Value, 1, 0)
    item.Value("hw") = If(wnd1_1.thw(i).Value, 1, 0)
    item.Value("de") = wnd2_1.tde(i).SelectedRowIndex.ToString
  End If
  
  json.Value("val").Append(item)
Next

Dim jsonString As String = json.ToString

This code creates a JSONItem object and sets its properties using the provided data. The ToString method is then used to obtain the JSON string. This approach is more structured and less error-prone than manually building the JSON string.

4 Likes

Please don’t do this. Most Question and Answer reddits prohibit AI written answers. If someone wanted to ask AI, they could do so themselves.

I had spent the time to write up a post, also pointing out the failure in design that is the if ndx = 0 block, and here you’ve gone and made me not want to even live anymore.

2 Likes

Note that the JSON you produced is not standard. JSON uses double-quotes, not single-quotes, to delineate strings.

1 Like

just wanted to point out that chatgpt is a great help when explaining features in a language you don’t master
just ask it (him ? her ?)

1 Like

Tim has a point but there is no rule against posting such results. They should be accredited, as you did, and properly vetted since results from AI bots can often be wrong.

When Reddit has better moderation practices than you do it’s worth stepping back to reflect.

Q&A places with anti-AI rules aren’t to deny anyone from using AI to supplement a decent, useful, helpful answer; but they are to prevent “ask AI to regurgitate some arguably poor code that doesn’t explain things to OP and post it just to be the first”.

1 Like

I understand, but the ArduinoJson Library parses it just fine. I’m glad because it gets really hard for me to read when you have to double quote everything.

Thank you for helping me sort this out. I will work with it when I get back.
As far as chat GPT goes, In my humble opinion chat GPT is a cancer on our society and I will try to avoid it like well cancer.

The native JSONItem class will produce standard JSON though, which will differ from what you are producing manually. I wanted to underscore that in case your intended target, for some reason, rejects standard JSON. (I seriously doubt it would.)

2 Likes

Thanks again, It takes it both ways.

Thanks again, It works perfectly and I see now where I was going wrong.

1 Like

you’re welcomed !

ChatGPT is a very nice tool. like any programming language.
it’s not God, and it’s not the Devil, it’s somewhere in between…
you must learn how to use it for your needs, learn how to talk to it to get the right answers.
ChatGPT will not steal the programmer’s jobs
but programmers that know how to use it will steal the jobs of those who don’t know how to use it.

I hear what you are saying, however, you are looking at it from a good side. It’s the bad side and the bad people that are going cause serious problems in society. My opinion.

I spoke to soon. I thought I was running the new code. I am getting an error when I try to run it.

json.Value("val").Append(item)

Type "Variant" has no member named "Append"

Any ideas?

Try

json.Value("val") = New JSONItem("[]")

That creates a JSON array, not just a key/value pair.

I still get the same error.

What I have done is create a dictionary array using an example from the help file, then convert that to json and that is working. The only problem is I still cant figure out how to get the first key ‘mod:1’ included.

Var valArray() As Dictionary
Var item As Dictionary


if mods = 1 then
  
  For i As Integer = 0 To 3
    'item = New Dictionary
    
    item = New Dictionary
    
    
    If ndx = 0 Then ' tabpanel 0
      item.Value("hx") = wnd1_1.tHtgMx(i).Text
      item.Value("hn") = wnd1_1.tHtgMn(i).Text
      item.Value("hd") = wnd1_1.tHtgDif(i).Text
      item.Value("cx") = wnd1_1.tClgMx(i).Text
      item.Value("cn") = wnd1_1.tClgMn(i).Text
      item.Value("cd") = wnd1_1.tClgDif(i).Text
      item.Value("al") = If(wnd1_1.talm(i).Value, 1, 0)
      item.Value("st") = If(wnd1_1.tst(i).Value, 1, 0)
      item.Value("hw") = If(wnd1_1.thw(i).Value, 1, 0)
      item.Value("de") = wnd1_1.tde(i).SelectedRowIndex.ToString
    Else ' tabpanel 1
      item.Value("hx") = wnd2_1.tHtgMx(i).Text
      item.Value("hn") = wnd2_1.tHtgMn(i).Text
      item.Value("hd") = wnd2_1.tHtgDif(i).Text
      item.Value("cx") = wnd2_1.tClgMx(i).Text
      item.Value("cn") = wnd2_1.tClgMn(i).Text
      item.Value("cd") = wnd2_1.tClgDif(i).Text
      item.Value("al") = If(wnd1_1.talm(i).Value, 1, 0)
      item.Value("st") = If(wnd1_1.tst(i).Value, 1, 0)
      item.Value("hw") = If(wnd1_1.thw(i).Value, 1, 0)
      item.Value("de") = wnd2_1.tde(i).SelectedRowIndex.ToString
    End If
    
    valArray.Add(item)
    'json.Value("val").Add(item)
  Next
  wndMainV1.addToListBox(1)
end if 

Var json As String
json = GenerateJSON(valArray)

generates this

[{"hx":"75","hn":"60","hd":"10","cx":"0","cn":"0","cd":"0","al":0,"st":1,"hw":0,"de":"1"},
{"hx":"60","hn":"50","hd":"5","cx":"0","cn":"0","cd":"0","al":0,"st":0,"hw":1,"de":"4"},
{"hx":"160","hn":"110","hd":"20","cx":"0","cn":"0","cd":"0","al":0,"st":1,"hw":0,"de":"3"},
{"hx":"0","hn":"0","hd":"0","cx":"0","cn":"0","cd":"0","al":0,"st":0,"hw":0,"de":"0"}]

I have figured out a way to work around the mod:1 key/value. So using it with the dictionary method seems to work OK. It has achieved what I was hoping by making it more readable and has some error checking built in. It really makes it nice since I am working with 8 modules. Thanks again for all of the help with getting this headache resolved.

Here is the final code.

Var valArray() As Dictionary
Var item As Dictionary

if mods = 1 then
  For i As Integer = 0 To 3
    item = New Dictionary
    If ndx = 0 Then ' tabpanel 0
      item.Value("hx") = wnd1_1.tHtgMx(i).Text.ToInteger
      item.Value("hn") = wnd1_1.tHtgMn(i).Text.ToInteger
      item.Value("hd") = wnd1_1.tHtgDif(i).Text.ToInteger
      item.Value("cx") = wnd1_1.tClgMx(i).Text.ToInteger
      item.Value("cn") = wnd1_1.tClgMn(i).Text.ToInteger
      item.Value("cd") = wnd1_1.tClgDif(i).Text.ToInteger
      item.Value("al") = If(wnd1_1.talm(i).Value, 1, 0)
      item.Value("st") = If(wnd1_1.tst(i).Value, 1, 0)
      item.Value("hw") = If(wnd1_1.thw(i).Value, 1, 0)
      item.Value("de") = wnd1_1.tde(i).SelectedRowIndex
    Else ' tabpanel 1
      item.Value("hx") = wnd2_1.tHtgMx(i).Text.ToInteger
      item.Value("hn") = wnd2_1.tHtgMn(i).Text.ToInteger
      item.Value("hd") = wnd2_1.tHtgDif(i).Text.ToInteger
      item.Value("cx") = wnd2_1.tClgMx(i).Text.ToInteger
      item.Value("cn") = wnd2_1.tClgMn(i).Text.ToInteger
      item.Value("cd") = wnd2_1.tClgDif(i).Text.ToInteger
      item.Value("al") = If(wnd1_1.talm(i).Value, 1, 0)
      item.Value("st") = If(wnd1_1.tst(i).Value, 1, 0)
      item.Value("hw") = If(wnd1_1.thw(i).Value, 1, 0)
      item.Value("de") = wnd2_1.tde(i).SelectedRowIndex
    End If
    valArray.Add(item)
  Next
  wndMainV1.addToListBox(1)
end if 

Var json As String
json = GenerateJSON(valArray)


The IF statements in the lower block check properties of wnd1_1, not wnd2_1 like the rest. Just want to make sure that was intentional.

You are right, That would have shown up eventually.“What the…”

1 Like