API Rest and JSon

Hi everyone,

I need your help preparing to send invoices (headers and lines) via a REST API.

I need to construct a request body containing an array of headers (even if it consists of just a single element); each header is made up of a set of properties and an array of lines.

Since I don’t have much experience with JSON, I’m struggling to put this together.

Can anyone help me?

Thanks!!

Nedi

What have you tried? What issue(s) are you seeing? What’s the REST API look like (documentation)? Have you taken a look at the REST API example included with Xojo?

desktop app?

have a look at class URLConnection (there are fragments for send JSON too)

the http header becomes
Content-Type: application/json; charset=utf-8
and the count of bytes of the body
Content-Length: 1234
the end of a raw header are 2x CRLF
followed by body data.

the json object is send as .ToString in the request body

Thank you, guys!

I had to create a body with this structure:

Document

array of Document_Header

value 1
value 2
value 3
…
array of Document_Line
value 1
value 2
value 3
…

I don’t know how to manage the various JSonitem to do that.

It works a lot like Dictionary, but can act like an array too.

I might do something like:

Var Headers As New JSONItem("[]")
Headers.Add("value 1")
Headers.Add("value 2")
Headers.Add("value 3")

Var Lines As New JSONItem("[]")
Lines.Add("value 1")
Lines.Add("value 2")
Lines.Add("value 3")

Var Document As New JSONItem("{}")
Document.Value("Document_Header") = Headers
Document.Value("Document_Line") = Line

Two things to note here:

  1. This is probably not correct for your implementation, but I’m working with the information I have.
  2. Passing "[]" and "{}" is absolutely not necessary. I like to do it as a hint to my future self that the item should be an array vs a structure. It also prevents you from accidentally switching the JSONItem to the other mode before any content is added.

Thank you, Tom. The correct structure of the body must be like this:

{
“DocumentiT”: [
{
“key_documento”: “…”,
“num_anno”: …,
“des_num_doc”: “…”,
“DocumentiR”: [
{
“key_documento”: “…”,
“prg_riga”: …,
“ind_tiporiga”: “A”
}
]
}
]
}

The problem is that I don’t know how to obtain it!

Here’s the “pretty” version, for anybody else reading:

{
	"DocumentiT": [
		{
			"key_documento": "…",
			"num_anno": "…",
			"des_num_doc": "…",
			"DocumentiR": [
				{
					"key_documento": "…",
					"prg_riga": "…",
					"ind_tiporiga": "A"
				}
			]
		}
	]
}

I’ve given you the basic premise. You’re going to need to create a JSONItem struct with the keys key_documento, prg_riga, and ind_tiporiga. You then add that to a JSONItem array, and add that array as the value for a DocumentiR key in a different struct. And so on. There’s definitely some nesting to do.

If you mean you don’t know how to get the JSON string to send over the socket, a quick look at the documentation would point you to the ToString method. Its default parameters will serve just fine for a REST API.

JSONItem

to understand jsonitem create one and set a break point to see content in debug window.

explained json

Search Results for “json” – Xojo Programming Blog :slightly_smiling_face:

Thank you all, guys!