Multiple query results in API Action

Hi Wappler,

I am trying to insert multiple db query results in an API Action as JSON.

So if you see below- I am trying to get multiple shareholders and directors from a database query and insert the details in below json api action:

{
"metadata_version": "4",
"metadata_serviceUid": "xx",
"metadata_secretKey": "api_sec_x_xxx",
"type": "privateCompany",
"fields": {
"country": "AUS",
"abn": "16123456789",
"acn": "123456789",
"name": "Pass My Company",
"registeredOffice": "23 sdsd Street, sdsds NSW 2222",
"principalPlaceOfBusiness": "23 sdsd Street, sdsds NSW 2222"
},
"stakeholders": [
{
"type": "individual",
"role": "shareholders",
"fields": [
{
"firstName": "Jane",
"middleName": "ok",
"lastName": "Citizen",
"gender": "female",
"dateOfBirth": "11/10/1980",
"streetNumber": "23",
"streetName": "sdsd Street",
"suburb": "sdsds",
"postcode": "2222",
"state": "NSW",
"email": "email@gmail.com",
"country": "AUS"
}
]
},
{
"type": "individual",
"role": "directors",
"fields": [
{
"firstName": "Dave",
"middleName": "ok",
"lastName": "Citizen",
"gender": "male",
"dateOfBirth": "20/10/1980",
"streetNumber": "23",
"streetName": "sdsd Street",
"suburb": "sdsds",
"postcode": "2222",
"state": "NSW",
"email": "email@gmail.com",
"country": "AUS"
}
]
}
]
}

I have been banging my head against a wall for hours in this one.. any urgent help would be greatly appreciated :pray:

Use Group step and Repeat step to build your JSON

API Action JSON data will only have one line, to inject the Group step variable

Try to search my forum posts containing the word Group, perhaps I've explained with more detail before

Thank you for your help! Does this look correct?

Slightly different from what I had in mind: I had in mind the Repeat shareholders goes inside the Group step

And take out the Send Mail step from inside the Group

Thank you. Like this? I took out the send mail and put it after as I want to test the results. How do I access the results? {{shareholders}} ?? Thats blank when I email it to myself

Please rename the Group shareholders to "json" or something else, as there's a variable name collision with the Repeat shareholders

My idea is this Group "json" would have all Set Values required to build the JSON, including the Repeat shareholders you put it there nicely

Then you could indeed access {{ json }}, which would have the shareholders property inside

If you get it empty, maybe you need to add a JSON encode formatter (not in the UI):

Ok.. so result I am getting of {{json}} being emailed is [object Object]

Whats more confusing is there needs to be a fields key.. so the correct JSON structure for the API call is this: "stakeholders": [
{
"type": "individual",
"role": "shareholders",
"fields": {
"firstName": "John",
"middleName": "A",
"lastName": "Doe",
"gender": "male",
"dateOfBirth": "1980-01-01",
"streetNumber": "123",
"streetName": "Main Street",
"suburb": "Sydney",
"postcode": "2000",
"state": "NSW",
"email": "john.doe@example.com",
"country": "AUS"
}
},

and I need the info under fields to be populated by the Group

You need to use the JSON encode formatter

Create a Group called "fields", with the relevant Set Values inside

I think you should create a new Server Action where you just play with the Group step and Repeat till you're able to create the JSON representation you want

Thank you. How do I use the json formatter as its JavaScript? Or can I just append it to the name ie xxxx.JsonStringify()

{{ json.toJSON() }}

In the post I linked it's JavaScript because I poked into Wappler's framework source-code

1 Like

Aaah thank you. I will give this a shot

1 Like

You are a life saver! Worked perfectly. Thank you so much for your time, I really appreciate it!! For anyone who needs a solution similar to this:

Happy to hear that! You're welcome! :smiley: