Add data to JSON on Webserver

Hi everyone,

from a windows desktop-app i try to add data to json file.
I succeded with reading the file and downloading the json data with

Var jsString As String
Try
  jsstring = licenseConnect.SendSync("GET","https://mydomain.de/abc/abc.json", 30)
  var jsonArray() as Variant 
  jsonArray=ParseJSON(jsString)
  
  for i as Integer=0 to jsonArray.Ubound
    var l as new cLizenz
    l.serial=Dictionary(jsonArray(i)).value("serial")
    l.macID=Dictionary(jsonArray(i)).value("macID")
    l.anzahl=Dictionary(jsonArray(i)).Value("count")
    currLizenz.append(l)
  next i
Catch ErrorException
  MsgBox "No connection!"
End Try

So far so good…

But how can i add data to the json and post it to the webserver?

If you turn the server’s item into a Xojo JSONItem you can manipulate it and send it back. You’ll probably want a method on your cLizenz class that turns the contents back into a JSONItem for communication back to the server.

var oSock as new URLConnection
var sData as String = oSock.SendSync("GET", "https://url")

try
  var jsData as new JSONItem(sData)
  // Do your manipulations on jsData
  // Send the data back with another URLConnection

catch ex as JSONException
  // Something happened

end try

These doc pages will help a lot:
https://documentation.xojo.com/api/text/json/jsonitem.html
https://documentation.xojo.com/api/networking/urlconnection.html

Let us know if you get stuck!

Hi Tim,
thanks for your reply.

But this is exactly the problem i don’t have a solution for:

I’m using the UrlConnection for my example, but i don’t know how to “send the data back”. Don’t i have to use “POST” instead of “GET”, or maybe i misunderstood your reply…

I know how to manipulate the JSON-file, but i don’t know how to send it back to the webserver.

Ah, we misunderstood each other. Or at least, I didn’t quite understand where you were stuck. But I do now!

You do have to use POST. You’ll want to set the request contents of the URLConnection object. Check out the docs on URLConnection and there’s an example project at:
<YourXojoPath>/Example Projects/Communication/Internet/URLConnection/URLConnectionPOST.xojo_binary_project

If you have any interest in an already existing end-to-end Xojo code DRM, it is a product and service I offer. More information on TPLM is available here :slight_smile:

Thanks a lot, Tim.
I’ll look at the link you posted. If i won’t succeed with this i’ll think about your solution :wink: