Server API post JSON data array

I need to POST data from an API source, to another API destination. Fully server side.

Getting the data from the API source is no problem. The POST to the destination should be something like this:

[
    {
        "client_id": 113974,
        "table_name": "customers",
        "sequence": 1565881320,
        "key_names": [
            "id"
        ],
        "data": {
            "id": 1,
            "name": "Finn"
        },
        "action": "upsert"
    }
]

Especially the array’s are a problem, I’m not sure how to process this in Wappler. Any docs that can point me in the right direction?

Hey Nevil,

With a json data type you can build whatever you need:

For an array, just reference any array. For one example the account_ids below is expecting an array of values.

{
  "client_id": "{{queryPlaidConfig[0].client_id}}",
  "secret": "{{queryPlaidConfig[0].secret}}",
  "access_token": "{{queryGetToken[0].access_token}}",
  "start_date": "{{$_POST.start_date.formatDate(\"Y-MM-dd\")}}",
  "end_date": "{{$_POST.end_date.formatDate(\"Y-MM-dd\")}}",
  "options": {
    "count": "{{batch_size}}",
    "offset": "{{offset}}",
    "account_ids": "{{queryGetAccounts.groupBy('account_id').keys()}}"
  }
}

–Ken

Thanks for your help Ken, I noticed that your arrays are always between {{}} , is dat a requirement for arrays to work?

In my example the arrays are actually the first and last thing, outside a {{}}. Should I format it differently?

Hey Nevil,

Here is how I look at this.

{
  "client_id": "{{queryPlaidConfig[0].client_id}}",
  "secret": "{{queryPlaidConfig[0].secret}}",
  "access_token": "{{queryGetToken[0].access_token}}",
  "start_date": "{{$_POST.start_date.formatDate(\"Y-MM-dd\")}}",
  "end_date": "{{$_POST.end_date.formatDate(\"Y-MM-dd\")}}",
  "options": {
    "count": "{{batch_size}}",
    "offset": "{{offset}}",
    "account_ids": "{{queryGetAccounts.groupBy('account_id').keys()}}"
  }
}

The entire block above needs to end up as valid json for it to work, and further it has to be what is expected by the api.

So the single braces (shown here in white) are a part of that json.

The paired sets of left and right curly braces basically say “evaluate what is between us and replace everything (in cluding the curly braces) with what we return”.

Some of those evaluations will result in text strings, and some (not all) will be arrays. And if an array, it will by definition be enclosed with square brackets.

In my example, {{queryGetAccounts.groupBy(‘account_id’).keys()}} will return [123,456,789] which we can see matches the format your json is expecting for key_names. Don’t get too hung up on my groupby and kets formatters as that is just one specific way to get an array.

Yours will be something like this:

[
    {
        "client_id": {{client_id}},
        "table_name": "customers",
        "sequence": 1565881320,
        "key_names": {{your_query.data.array}},
        "data": {
            "id": {{data_id}},
            "name": "{{data_name}}"
        },
        "action": "upsert"
    }
]

You will have to replace my examples with your actual names and choose when something is static (no curly braces) and what is dynamic.

–Ken

1 Like

Thank you for your extensive reply, the problem is that if I do a simple JSON with just static values, lets say:

{
  "client_id": "test123",
}

image

I get NULL:

While if I set the data type to auto:

image

I get succes:

But I think it’s a bug related to wappler?

Your json is not valid…remove the comma and it should work.

Facepalm. That’s what you get when a non-coder starts fiddling with code :smiley:

{
  "client_id": "test123"
}

Produces succes.

[
{
  "client_id": "test123"
}
]

Produces this in webhook.site:

"[{\n \"client_id\": \"test123\"\n}]"

Is that supposed to happen? I’m just keeping it real simple for now, just to figure it out.

That is json with escaped values so probably fine.

It’s not easy to test/debug when it’s formatted like that, but I’m not sure what is going wrong here. This is what I did in Postman, which works fine:

When I first create the server action in Wappler, I enter exactly the same data:

But when I press refresh within Wappler (in the server actions panel), suddenly this screen looks like this:

I then cannot enter a query/header anymore. I can still run this server action, but it produces an error from the API destination. While it’s exactly the same code as far as I can see.

1. status: "ERROR"
2. message: "An array of records is expected"

Ya, this is not looking right. If it were me, I would start from scratch to avoid the back and forth of trying different things. Just start a new server action to troubleshoot.

Yes that’s what I did in the above example. I have a bug report here, so hopefully the team can tackle it

Hi @nevil,

I’m aware of the query & header field going unresponsive and needs to be fixed.

Try this and let me know if it works:

  1. Enclose the whole json data with {{ }} and assign it to a variable. When pasting this data to the variable, remove all line & para breaks (I use this online page - https://www.textfixer.com/tools/remove-line-breaks.php). When you paste it to the variable (pencil icon), it should automatically classify this as “text” data & not “json” (it is still json within)
Set Value json_data = {{
[
    {
        "client_id": 113974,
        "table_name": "customers",
        "sequence": 1565881320,
        "key_names": [
            "id"
        ],
        "data": {
            "id": 1,
            "name": "Finn"
        },
        "action": "upsert"
    }
]
}}
  1. In your api call, set Data Type to JSON & set the JSON Data to the above variable like this:

JSON Data: {{json_data}}

Passing it through the variable avoids the unresponsive query & header fields issue. But, not sure if it’s going to work for you, I had something like this and made it work after long struggle… Let me know how you go!

Okay I did like you said, created a variable json_data and put the JSON in between {{}}:

image

But it still creates the query/header issue:

Running the server action in the front-end produces this error:

{
   "code":0,
   "file":"\/var\/www\/html\/dmxConnectLib\/lib\/core\/Lexer.php",
   "line":70,
   "message":"Unexpected character \n at column 0 in expression {{\n[\n    {\n        \"client_id\": 113974,\n        \"table_name\": \"customers\",\n        \"sequence\": 1565881320,\n        \"key_names\": [\n            \"id\"\n        ],\n        \"data\": {\n            \"id\": 1,\n            \"name\": \"Finn\"\n        },\n        \"action\": \"upsert\"\n    }\n]\n}}",
   "trace":"#0 \/var\/www\/html\/dmxConnectLib\/lib\/core\/Parser.php(84): lib\\core\\Lexer->parse('\\n[\\n    {\\n      ...')\n#1 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(221): lib\\core\\Parser->parse('\\n[\\n    {\\n      ...', NULL)\n#2 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(206): lib\\App->parseObject('{{\\n[\\n    {\\n    ...', NULL)\n#3 \/var\/www\/html\/dmxConnectLib\/modules\/core.php(99): lib\\App->parseObject(Object(stdClass))\n#4 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(173): modules\\core->setvalue(Object(stdClass), 'json_data')\n#5 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(137): lib\\App->execSteps(Object(stdClass))\n#6 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(107): lib\\App->execSteps(Array)\n#7 \/var\/www\/html\/dmxConnectLib\/lib\/App.php(72): lib\\App->exec(Object(stdClass))\n#8 \/var\/www\/html\/dmxConnect\/api\/Sync\/QLS\/json_test3.php(8): lib\\App->define(Object(stdClass))\n#9 {main}"
}

Hi @nevil,

If you removed the line breaks & para breaks as per my above suggestion and then assign your data to the variable, your variable will look like this (it will go into one line and you scroll horizontally). It will automatically classify it as “Text” on the top right corner.

image

Try this. When I pasted your json within double curly brackets in my previous reply, the forum’s block tool automatically beautified the json.

Your json without line & para breaks will look like this:

{{ [ { “client_id”: 113974, “table_name”: “customers”, “sequence”: 1565881320, “key_names”: [ “id” ], “data”: { “id”: 1, “name”: “Finn” }, “action”: “upsert” } ] }}

I have a similar setup and it works at my end, but every api is different. I inferred that wappler still has to perfect the newly introduced json data thingy, and the above text method is a workaround till then…

In the newest version 2.5.6 (just released few minutes ago) it gives me this error:

image

This is a new validation on Wapplers side I think @Teodor ? It does allow me to save the action file (I see the green notification on the right), but it doesn’t save the JSON data field text I input there.

Hello Nevil,
This input expects you to paste your JSON directly there.

Hi @Teodor ,

This breaks how things were working normally earlier. The JSON data might come from another api source (or) may be built using various data on server side (or) pulled from a database (or) might be an input from the client-side.

If the json data can’t be passed through a variable in the API call (which worked fine till last version), then it will break my current site and I’m guess many other’s as well.

Ah yes sorry, my mistake - it should accept dynamic data indeed.
@George will check this.

1 Like

try to enclose it in double quotes, like:

"{{json_data}}"

Pasting my JSON directly in there doesn’t work also, it outputs the following:

"[\n {\n \"client_id\": 113974,\n \"table_name\": \"customers\",\n \"sequence\": 1565881320,\n \"key_names\": [\n \"id\"\n ],\n \"data\": {\n \"id\": 1,\n \"name\": \"Finn\"\n },\n \"action\": \"upsert\"\n }\n]"

While it should output this:

[
    {
        "client_id": 113974,
        "table_name": "customers",
        "sequence": 1565881320,
        "key_names": [
            "id"
        ],
        "data": {
            "id": 1,
            "name": "Finn"
        },
        "action": "upsert"
    }
]