Server action - convert json to array

Hi community,

I have a nested json submitted to server-side by client-side form made up of hidden input fields. The format of the json is like this:

myEmployees: [
        { name: "John", role: "Analyst"},
        { name: "Kate", role: "Manager"},
        { name: "Jack", role: "Assistant"},
        { name: "Tom", role: "Contractor"}

I want to extract the names as an array like below, and use this further for further processing.

[“John”, “Nick”, “Jack”, “Tom”]

Any idea how to do this? I did try running the json through a repeat, and assigning the names to another variable and outputting it prints both key-value pair, not just the value.

Wish json & array processing was simpler and easy in wappler on server side, it has always been a mystery! Any help will be very appreciated!

I don’t have anything setup where I can test this, but I think it will be:


Thanks a bunch for your suggestion @mebeingken… here is the real data I’m trying to apply the above logic:

When I do {{all_filters.groupBy(‘filter’).keys()}}, I’m getting the following error:

code: 0
file: “/Users/akayy/Wappler/Zorrobot/zorroweb_alpha/dmxConnectLib/lib/formatters/collections.php”
line: 73
message: “Undefined index: filter”
trace: "#0 /Users/akayy/Wappler/Zorrobot/zorroweb_alpha/dmxConnectLib/lib/formatters/collections.php(73)

Any idea why I’m getting this error? Thanks again!

Hey. So I try not to ask too many questions as to why developers do things, but I gotta ask…are you sure this is the best way to handle all this? I mean, you are starting with hidden inputs containing json, and then trying to manipulate that json on the server…can you not just send these values in as arrays so you can just use the tools available? Anytime I spend a lot of time on something, it inevitably exposes that I have a root problem that needs to be addressed/restructured.

At any rate…the values you are showing for elements within your all_filters list do not represent true json - the key should always be in double quotes. So filter should be “filter”. Is that triggering the error, not sure.

Thanks mate. I will definitely re-look at the solution design, but long story short, the all_filters is part of a larger form submission (i agree it’s not a json, it’s an array of objects) and I have to dynamically build multiple elastic-search queries (in true json format).

I really want to understand why your suggestion is not working and what’s the underlying difference. I did a thorough search on the forum and noticed, you have dealt with similar scenario here… and patrick’s solution seemed to work for you.

The all_filters at my end is part of a repeat in a server action, while yours seems to be something returned from a query/api-call. And I also notice that the account_id in your above is not within double-quotes anyway.

My form submits the below filters (array of objects).

In my server action, I put the filters through a repeat (named it all_filters) and output the filter_name conditionally if tagged true.

Then, using {{all_filters.groupBy('filter').keys()}}, I want to output an array like this
["Last 24 hours", "Last 48 hours", "Custom"], which is not working :pensive:, may be @patrick can help when he is back.

Been staring at this and just can’t see what we are missing. All seems correct to me. What does all_filters output look like?

all_filters looks like this:

Applying {{all_filters.groupBy('filter').keys()}} should give me the array of values, but it gives me this error:

code: 0
file: “/Users/akayy/Wappler/Zorrobot/zorroweb_alpha/dmxConnectLib/lib/formatters/collections.php”
line: 73
message: “Undefined index: filter”
trace: "#0 /Users/akayy/Wappler/Zorrobot/zorroweb_alpha/dmxConnectLib/lib/formatters/collections.php(73)

This issue could be related to the below, I just want to make sure I raise this as a defect, if it is in fact one.

  • There is definitely a difference between repeating an array (returned by form-submission, db-query or api-call) using the same name versus using a custom-name.

  • If I repeat the above “filters” with the same name, I can output all the properties. But when I repeat the same “filters” using a custom-name like “all_filters”, then I can’t output the properties.

  • Either way, the above groupBy formula didn’t work giving me the array of values.

Thanks for looking at this Ken, much appreciated. Hopefully I can get past this soon! Cheers!

Maybe you are using the wrong quotes. As I recall grouping is working with dynamic expression so use the back ticks instead of regular single quotes.

If you just use the picker dialog and formatter instead of hand coding all quotes will be correctly inserted.

I might have figured out what the problem is. My all_filters repeat loops through a couple of array-of-objects and one of them is not well-formed for the collections formatter to work on, hence spitting the error message.

See below:

  • image-1 has the filters (form-submission)
  • image-2 is the if/else and since I’m filtering out only those filters tagged true, I have a dummy comment in the else part
  • image-3, this else part is in fact outputting an empty array instead of not doing anything

How can I avoid this and have a pure “if” condition without “else” part?

When formatter is applied to the above all_filters, I get the below error: message: “Undefined index: filter”, which now makes sense as two of the arrays are empty without a filter key/value pair.

I’ve used this before where I went ahead and set the same value but used a static bogus value and then filtered out.

You can also use a ternary syntax

valueA == valueB ? If true : if false
1 Like

Thanks Ken, removed the conditional loops & used the “where” collections formatter like this.

It worked, yaayyy, thanks again for the support Ken & George… great learning!

1 Like