Server API post, How do I create JSON input from a query (multiple records)

Hi everyone,

Here’s my scenario. I am issuing an API request to an AWS service and want to submit a list of questions and answers. Here is a sample JSON body.

{
“key”: “testkey”,
“faq”: [
{
“q”: “Question 1?”,
“a”: “Answer to Question 1.”,
“index”: 0
},
{
“q”: “Question 2?”,
“a”: “Answer to Question 2.”,
“index”: 1
}
],
“company”: “Test Company”
}

For the key and company fields I pass in $_POST variables. For q, a, and index I need to be able to query a “question” table that has q, a, and question_id as values. Query response is 1…n records per company.

How do I dynamically create this list of question, answers and index and pass this json data to a server side submit request / action? Thanks for the thoughts, help and suggestions.

Let me know if you need any additional details.

Hi, I’m not sure if this will help but there could be some info there for you.

Basically any server connect file creates a JSON output that can be used like an API, but this trick above is what I needed to do to be able to use the data properly when using the data through a URL.

Useful to know if you don’t already, even if it doesn’t help here.

Thanks Phillip_J, this led me in the right direction. I didn’t realize that the output of the query was native json that could be subsequently referenced.

Here is what I did to get this to work.

  1. For some reason my query was not populating in wappler, I validated data existed in MySQL and I think it was some combination of naming the query and how I was referencing it. (Started over and left the default names)
  2. Created a new database query, with the default naming query1
  3. Used the alias table to ensure that the naming matched what my API was looking for, question_id -> index
  4. I no longer needed a repeat step as I thought I would need to build the string. I was able to reference {{query1}} as the input for the nested json string instead.
  5. Problem solved!

Thanks!

1 Like