question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pass a JSON string/object as args to a sub routine

See original GitHub issue

Is there any way to pass in a JSON string as an argument to a subroutine like this:

> + Do Something
> - <call>doSomething {action:"print", path:"/one/two"}</call>
> 

I tried a few ways to parse the args as a JSON object and pass it onto another function but I have been unable to get a JSON object that mirrors the JSON string as passed to the subroutine from the rivescript. Is there another approach I can take to accomplish the same?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ssc2015commented, Nov 16, 2016

@kirsle, I will be happy to help with the implementation of this new feature! We can further simplify the JSON schema where “object” key specifies the subroutine to be called and “params” contains an arbitrary key/value pairs as follows:

+ do something
- <call>{ "object": "doSomething", "params": {"action":"print", "path": "/one/two" } }</call>

Please let me know your thoughts on how I can contribute.

Regarding the workaround, I am not sure if it can handle concurrency since the uservar (tmp) could change between multiple invocation of the same trigger by the same user and could potentially run into one invocation getting the args of another invocation, right?

0reactions
ssc2015commented, Nov 17, 2016

I have been playing around and tried the following:

  • Passed in a JSON style string using single quotes & parenthesis (see below for an example)
  • The arg parser passed along the args string pretty much as is except one thing: it added an extra comma wherever it found a comma in the original string
  • In my subroutine, I simply replaced single quotes with double quotes, ‘(’ with ‘{’, ‘)’ with ‘}’, and double commas with a single comma.
  • Then I was able to parse the JSON string!

Here is one of the snippets I have tried.

! version = 2.0

+ json test * *
- <call>doSomething ('params':('props':('prop1':'val1', 'level2':('prop2':'<star1>', 'prop3':'<star2>'))))</call>

> object doSomething javascript
  var args2 = args.join('');
  args2 = args2.replace(/'/g, '"');
  args2 = args2.replace(/\(/g, '{');
  args2 = args2.replace(/\)/g, '}');
  args2 = args2.replace(/,,/g, ',');
  var json = JSON.parse(args2);
  return JSON.stringify(json);

< object

This approach does not require a major change and continues to keep the <call>tag sane and simple! What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass json object as a parameter to another method
A JSON object is not more than a regular javascript object. It should work if you pass it as a parameter to a...
Read more >
How to pass a json object as a parameter to a python function?
Practical Data Science using Python​​ We have this code below which will the given json object as a parameter to a python function....
Read more >
Passing JSON as parameters to JavaScript when there are ...
Passing JSON as parameters to JavaScript when there are nested objects. Good day. I'm trying to use FileMaker.PerformScript in a Web Viewer ...
Read more >
JSON argument - SecondState.io
Use JSON strings as function arguments. ... Rust is able to map a JSON string to a Rust object using what is known...
Read more >
Object functions for Bicep - Azure Resource Manager
If you need to include a parameter value or variable in the JSON object, use the concat function to create the string that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found