Pass arguments from piston as JSON


#1

I’ve got a piston like so that passes a bunch of variables to another piston

This then calls my Assistant Relay and should pass the arguments down

The issue is that it’s passing them down like so:
{ '$args': '[broadcast:true, command:hi, user:greg]' }

Is there a way I can pass them as JSON? Seems like its creating a new object with a key called $args and then converting the object from my first piston into an array

Assistant Relay accepts JSON, so would be looking for

{ 
  'broadcast': true,
  'command': hi',
  'user': 'greg'
}

#2

There are others that know a lot more than me but you might need to assign your args into variable like

string jasonData=$args

Or you might be able to build your JSON data using the json function. See the wiki here: json


#3

Tried that but it doesn’t seem to work.

If you pass send the individual variables as a JSON encoded web required, it’s fine, but as soon as they’re passed from another piston they’re turned into some weird array


#4

I tried this method… see if it works for you

and this is the result on beeceptor…

image


#5

I think the problem is I’m passing variables from another piston, and there’s no known amount of variables these could be.

They get passed to the piston in some way through the $args variable. [foo:bar, bar:foo] etc

I need to somehow process that as json before sending it to my endpoint


#6

I would assign the args to variables first (just because I like to see what is happening step by step)
Set variable {broadcast} = $args.broadcast
…etc

then use the custom body type as @eibyer suggested.

You’ll have to write as an expression

'\{"broadcast": {broadcast}, "command": {command}, "user": {user}\}' should translate to

{“broadcast”: true, “command”: hi, “user”: greg}

It might work to put the args directly into the expression
'\{"broadcast": {$args.broadcast}, "command": {$args.command}, "user": {$args.user}\}'


#7

image

image


#8

It took me a minute to get to that formatting. The headers broadcast, command and user need to be sent with quotes. So for webcore to process correctly as text with quotes, it has to be double quoted.

The easiest was to put the entire string in single quotes, the internal text in " ", and the initial { and final } escaped as those also need to be treated as text. The internal {} signify the use of the respective variables.


#9

OK, feel like I’m getting closer here, but there seems to be an issue with the null value in WebCore

Trigger Piston

Assistant Relay Piston

The problem now is that null is passed to the server as a string, and is therefore not actually null

{ command: 'Hello World',
  user: 'greg',
  converse: 'null',
  broadcast: 'true',
  preset: 'null' }

#10

If it’s blank, is it supposed to send “” ?, if yes, then try setting up the variables like below…

image


#11

Hi, do you found any solution? I m trying to pass arguments in url to update my variable with simple line of code… I would like something like: https://key/pistonid?name=[name1,name2,name3]&value=[value1,value2,value3]
Then get the anwser inside the piston as a json: name.count = (number of item in name) and then set each variable with its name with a kind of loop…
If I could just get: name[1] it would be nice but I just cant figure out how to pass a list or json inside the url…
Thanks!