Setting server side object variables

Thanks @s.alpaslan.

I’m actually triggering all this server-side in response to a webhook, so there isn’t any opportunity to build in with $_POST.

I finally realized however, that I can just edit my server api action in the editor and build the nested json I need! So, problem solved, but it would be nice at some point to be able to build these in the gui editor.

It ends up looking like this:

I was able to use the editor in order to get the “count” and “offset” elements added to the json, and this works great. I have one last piece though, that I can’t decipher.

I need to add the “account_ids” which is a list of strings. These values will come from a db query.

{
	"client_id": "{{client_id}}",
	"secret": "{{secret_key}}",
	"access_token": "ENTER_ACCESS_TOKEN_HERE",
	"start_date": "2017-01-01",
	"end_date": "2018-01-01",
	"options": {
		"count": 250,
		"offset": 100,
		"account_ids": ["223984798DJEKL", "SHLEI290500S"]
	}
}

I’m assuming I use some form of {{queryGetAccounts.account_ids}} but haven’t found anything that works in json.

Anybody know how do I reference a db query array, and maintain a proper json request?

@patrick can you help with this last piece? How to get a list into an api action input.

If {{queryGetAccounts.account_ids}} return the exact data needed then you can use it, just put it in a string like "account_ids": "{{queryGetAccounts.account_ids}}". If it is not in the correct format then perhaps one of the formatters can help, if you are missing a formatter then let us know.

Unfortunately, that returns null.

From the wappler editor, the api action (note, I had account_ids instead of account_id originally for the db query results, which was a typo):

Here’s the raw capture from webhook.site:

And here is what queryGetAccounts is returning:

Thanks for the help!

try {{queryGetAccounts.groupBy('account_id').keys()}}

Success!

"options": {
              "count": 500,
              "offset": 0,
              "account_ids": "{{queryGetAccounts.groupBy('account_id').keys()}}"
            }

Thanks Patrick…very much appreciate your time on this.

I’m exactly in the same scenario, any help will be greatly appreciated, stuck on this for almost two days :frowning:

I have constructed a custom nested json {{query_usersignals}} which I send as input data for my POST api call. Format was suggested by @George in @nevil’s thread here - Send nested JSON with server-side API. This whole json is constructed at server side only (not fetched through client POST) and my elastic search api needs needs this nested json (query DSL).

image

This is my nested json object {{query_usersignals}}. The below whole structure is further enclosed within {{ }} as suggested by George in the other thread.

{
        "bool": {
            "must": [
                {
                    "match": {
                        "userID": "qatest"
                    }
                }
            ]
        }
    }

I’m so glad to get this work. I can see the api query returning data for this userID “qatest” (hard-coded) in my browser. I expanded the results and verified that the above input data filter did work, so pretty good outcome so far.

Now, where I’m unable to progress anymore is when I wanted to pass the userID variable {{username}} in the above nested json. I did open the server action in the editor and tried replacing the hard-coded value (\“qatest\”) with the {{username}} variable. Tried various variants, removing quotes, etc, but always hitting a format error and I end up breaking this server action.

Too sleep deprived, would be great if the community can help. @Teodor @patrick @mebeingken

In the Set Value step remove the outer {{ }} from the value. So it would become:

{\"bool\":{\"must\":[{\"match\":{\"zorroId\":\"qatest\"}}]}}

or when using a expression then you need extra quotes:

{{'{\"bool\":{\"must\":[{\"match\":{\"zorroId\":\"qatest\"}}]}}'}}

Hi @patrick, thanks, thing is this expression is working fine… as it is… Removing the outer {{ }} breaks the api call.

What I wanted is, in this json, replace value “qatest” with a variable called {{username}}. When I try doing this by opening the server action php file in the editor, I get a formatting error on this page. Are you able to help me here please?

This is similar to what @nevil tried here - Send nested JSON with server-side API. And what @mebeingken tried in the above thread.

Do you get an error when replacing “qatest” with username?

{{{ \"bool\": { \"must\": [ { \"match\": { \"zorroId\": username } } ] } }}}
1 Like

Thanks Patrick! Feeling soo relieved that this is fixed… learning slowly with wappler, getting better day by day, thanks again!

In Wappler 2.3.4 it’s now possible to use nested JSON data structures directly in the Server API Connector UI.

2 Likes

This is simply amazing, thanks @Teodor

Hi @mebeingken, did you find a solution for your original question-1. I have a nested json post’d from client-side form to server-side. On the server side, I have to process this json and literally change one value conditionally.

image

To my limited knowledge, I couldn’t find any direct way like $_POST.search_config.terms[1].term_name = “newvalue”. My understanding is, the whole nested json has to be put through a repeat, update during the loop, and has to be re-assigned to a new similar nested variable with output ticked.

Is this the only way, would you know? Thanks!

Well, I actually moved on from that as #2 was solved. Is your json going to be in a textarea input within a post form?

Yes, followed this guide… Send nested JSON with server-side API, using hidden textbox and submitting via form post!

Just to clarify…That seems to describe using multiple inputs (one for each element,) not one with the entire json…which are you doing?

Tried posting entire json, but realised that I had to convert to string on client-side & then decode the json on server-side, and that was too much for me, as currently there are no wappler out-of-the-box formatters for this…

Hence, ended up implementing the multiple inputs technique… The above nested json object is on server side, automatically generated by linking the client-side form.

I have some processing to be applied on this json data, and has to happen on server side. So, trying to achieve that, but looks very tedious as I can’t directly initialise any value in this json data without looping using repeats.

I guess my first thought is – if this json is from a client side api, could you move it to server so you can just deal with it as output from the call?

Or, you can always go the custom formatter route.

1 Like