API Connector How to create array within POST Body (JSON)

Hi there,

I want to post a JSON structure of an invoice to an external PDF generator tool.

I need to create a POST Json body in the API Connection that is something like:

{
	"company": {
		"name": "Company Name",
		"adress": "123 little street"
	},
	"products": [{
			"name": "product 1",
			"quantity": 1,
			"unitPrice": 12
		},
		{
			"name": "product 2",
			"quantity": 2,
			"unitPrice": 12
		}
	]
}

I know how to add dynamic values using <>. So the first part would be:
{
“company”: {
“name”: “<company_name>”,
“adress”: “<company_address>”
},

}

Question is how to dynamically create an array of a list of invoicelines (products), resulting in the following key/value structure:

"products":[
{"name": "product 1",
  "quantity": 1,
    "unitPrice": 12
},
{"name": "product 2",
  "quantity": 2,
   "unitPrice": 24
}
]

When I try to use do a search for, and using each items value. I get:
[
{“name”: “product 1, product 2”,
“quantity”: 1, 2,
“unitPrice”: 12,24
}
]
So you to iterate each product object in the json structure.
Thanks for the help!

2 Likes

Get your list of line items, then do :formatted as text then inside “craft” your JSON object. There’s a reference "This [thing]'s field1, etc. which is the relevant object for that specific line item. Then make the delimiter , to separate each object.

If the line item is able to have quotes in it and other JSON-breaking characters, do :formatted as JSON safeafter each dynamic value (this also adds quotes around it so don’t manually more more on each side)

3 Likes

And heres how it looks in a workflow from one of my apps. I have a list of ID’s I need to send to a backend database and it iterates through the list into the array

Cheers - JW

4 Likes

Hi @johnnyweb ,

I am trying what you have explained but I run into the problem that I also put double quotes around the keys in my Json object. For example:
[{“Key”: [dynamic value]:formatted_as json safe}]

The receiving party gets the following payload

[{\“Key\”:“Valueofthiskey”}]

How can I parse the keys correctly?

1 Like

Might be helpful to have some screenshots so we can see what you are doing. as @tylerboodman says if you’ve got double quotes use JSON Safe. Is the value you are trying to send a string or a number?

1 Like

I fixed it. I had the json safe function on the wrong level. It also eacaped the double quotes used on the keys. Using it on each specific dynamic value (as mentioned before) made it work. Thanks

1 Like

The power of rubber duck debugging - nice one :slight_smile:

2 Likes

Hi guys! Thank you very much for this thread.
I’m using the same function, but apparently, numbers fields (quantity and unit_price) are returned as text when creating the call with “formatted as text” function and it gives me an error for those specific fields.

I tried the generated JSON in Postman and it works perfectly.
I also run the call without the “formatted as text” function (using only 1 item) and it also worked as expected, so I assume the problem is when using that function, that numbers are read as string.

The items must be inside an array, maybe that’s the problem?
How could I make it work?

Thanks a lot!

2 Likes

@leq_24 You have a wrong double quote on key unit_price.
You have “unit_price:” but it should be “unit_price”:

Also it looks like you create something like:
{
“items”:[{{”title":“example 1”,
“quantity”:1,
“unit_price”:10.00,
“currency_id”:“ARS”},
{”title":“example 2”,
“quantity”:2,
“unit_price”:20.00,
“currency_id”:“ARS”}
}]
}

But I suppose it should be:

{
“items”:[{”title":“example 1”,
“quantity”:1,
“unit_price”:10.00,
“currency_id”:“ARS”},
{”title":“example 2”,
“quantity”:2,
“unit_price”:20.00,
“currency_id”:“ARS”}
]
}

Also I don’t know if you use the api connector plugin. But I did and I would prep the body in the api connector plugin like this

{
“items”:[<item_object>]
}

This creates a dynamic body parameter that you can assign in your workflow.

In there I would do someting like

Item_object = Rg each item formatted as text.

Formatted content:
{”title": value fromatted as json safe,
“quantity”:value,
“unit_price”: value
“currency_id”:“ARS”}

2 Likes

Thank you very much! It as really helpful!!
I swear I checked everything twice but missed that mistake.
Thanks!

2 Likes

This was great! Thanks for sharing!

1 Like