AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Use external variables (export values) in Chatscript
 
 

Hey. What I was looking for is to read values from an external source. Can I use Chatscript variables to fetch values from JSON objects or from any other structured databases that is not necessarily inside CS?

 

 
  [ # 1 ]

^jsonopen reads json from a website and brings them in as json facts (see Json manual).

 

 
  [ # 2 ]

You are looking for the following manuals:

JSON
POSTGRES
MYSQL
MONGODB
Javascript
External Access portion of the systems manual

 

 
  [ # 3 ]

Thanks guys for quick replies. I was focusing on getting things working with JSON.

Using the documentation pages & the one example I found on this forum, I tried this script, but it didn’t work for me (it returns a gambit instead)

outputmacro: ^GetJsonValue () 
$$user_agent = ^“User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)”
$$url = ^join(https://api.github.com/users/test/repos)  # limit 1000 requests/day
$$jsonId = ^jsonopen(GET $$url “” $$user_agent 8000)
      $$jsonVal = ^jsonpath( .name jo-0)
      Value of variable fetched from json is $$jsonVal

u: (<< “this must return json value” >>)

      ^GetJsonValue()

Any suggestion or a working example would be much appreciated (there aren’t any in the documentation pages). Thanks.

 

 
  [ # 4 ]

So if you did :trace all, you would find that the system has no trouble actually getting your json value.
BUT… you are assuming that jo-0 is a valid json object.  As you are doing a TRANSIENT json call, all things will be labelled as such, so at best it would be jo-t0.  BUT why would you NOT simply use the value of $$jsonID in your jsonpath call?

 

 
  [ # 5 ]

I tried to use the $$jsonID value but I haven’t got it to working.

Let’s say I’ve this JSON value:

[{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
}
}]

And I read the value in $$jsonid like this

$$jsonid = ^jsonopen(GET $url_apikey “” “”)

Then what should I do to access the first element of firstname or address.city?

I tried $$value = ^jsonpath($$jsonid[0].id.firstname) OR ^jsonpath(.id jo-0) ... I couldn’t find the result. I don’t understand the ja-0 and jo-0, but I’d love to hear if there is an easier way to access the return of jsonopen and jsonpath.

Thanks!!!

 

 
  [ # 6 ]

You read an array of objects transiently.
Your data read from JSON is in $$jsonid (which is ja-t0).  You want the first element of the array which is an object and then the firstname from that object. So
^jsonpath(”[0].firstname” $$jsonid).
Jsonpath ALWAYS takes the root as the 2nd argument ($$jsonid) and the path as the first argument.

 

 
  [ # 7 ]

Here is my attempt to do the same as your suggestion. It returns a gambit instead. (I tested the rule.)

outputmacro: ^testing ()
$
$jsonid = ^jsonparse([{
           
"firstName""John",
           
"lastName""Smith",
           
"age"25,
           
"children"[],
           
"spouse"null,
           
"address"{
                
"streetAddress""21 2nd Street",
                
"city""New York",
                
"state""NY",
                
"postalCode""10021-3100"
             
},
         
}])
     $
$answers = ^jsonpath("[0].spouse" $$jsonid)

u: (<< ... >>)

^
testing() 

 

 
  [ # 8 ]

Again, doing :trace would probably be helpful for you, telling you what happened in here. either $$answers was set to null, and your code doesnt DO anything so there is no output to the user so of course it gambits. OR jsonpath code is faulty in the engine and issued a fail.  But w/o a trace, I’m presuming you didnt PRINT anything out and hence you got nothing.  You might add after $$answers this code
if (!$$answers) {result was null} else {the result is $$answers}

 

 
  [ # 9 ]

Thanks for your help. I think I should explore more on the different topics. I was in the rush to get this to working. And, by the way, it did pass the if-else when I was trying to did what you suggest. I guess there is a problem with the parsing I did or something. Also, the trace output is a bit large & I am trying to find my way in all of these. Thanks.

 

 
  [ # 10 ]

you can limit trace in a number of ways.  :trace ^testing is the shortest for your purposes

 

 
  [ # 11 ]

Thanks. Here is a return of trace.

....,....,....................User call ^testing():()
....,....,......................
System call ^jsonparse(`{"firstName": "John", "lastName": "Smith", "age": 25, "address": {"streetAddress": "21 2nd Street", "city": "New York", `,`,`,`"spouse"`,`:`,`null`,``,``)
....,....,........................
JsonParse Call{"firstName""John""lastName""Smith""age"25"address"{"streetAddress""21 2nd Street""city""New York""state""NY""postalCode""10021-3100"}"children": , "spouse"null}create jo-t0 firstName John x1002200 Created 256056
....,....,........................create jo-t0 lastName Smith x1002200 Created 256057
....,....,........................create jo-t0 age 25 x1002100 Created 256058
....,....,........................create jo-t1000 streetAddress `21 2nd Streetx1002200 Created 256059
....,....,........................create jo-t1000 city `New Yorkx1002200 Created 256060
....,....,........................create jo-t1000 state NY x1002200 Created 256061
....,....,........................create jo-t1000 postalCode 10021-3100 x1002200 Created 256062
....,....,........................create jo-t0 address jo-t1000 x1002400 Created 256063
....,....,........................create jo-t0 children spouse x1002200 Created 256064
....,....,........................
....,....,......................
NOPROBLEM ^jsonparse({"firstName""John""lastName""Smith) => `jo-t0` e.g. ( jo-t0 children spouse x1002200 ), e.g. ( jo-t0 address jo-t1000 x1002400 ), - size 3 facts
....,....,......................$
$jsonid = ^jsonparse(jo-t0)
....,....,......................System call ^jsonpath(`"
[0].age"`,`jo-t0`)

....,....,........................
....,....,......................FAILRULE ^jsonpath("
[0].age) => ``
....,....,....................
FAILRULE ^testing() => `` 

And here is the macro

outputmacro: ^testing ()
  $
$jsonid = ^jsonparse([{
     
"firstName""John",
     
"lastName""Smith",
     
"age"25,
     
"address"{
         
"streetAddress""21 2nd Street",
         
"city""New York",
         
"state""NY",
         
"postalCode""10021-3100"
     
},
     
"children"[],
     
"spouse"null
 }]
)
 
$answers = ^jsonpath("[0].age" $$jsonid)
 if (!
$answers
  there is no result 
 } 
   
else 
    

     the result is $answers 
    } 
 

 
  [ # 12 ]

Your original example was ^jsonparse([{  yet the trace shows ^jsonparse({
So the outer thing does not appear to be an array anymore. Hence your request for an array access failed.

 

 
  [ # 13 ]

I see. I did a clean build and all. Same thing. What is the problem that cause this?

 

 
  [ # 14 ]

^jsonparse evaluates its argument.  You needed to put double quotes around it. [ ]  is treated as the output randomly pick choice.

 

 
  [ # 15 ]

It did get the first brace, but not the last when I enclosed it with quote.

....,....,....................User call ^testing():()
....,....,......................
System call ^jsonparse(`" [ { "firstName"`,`:`,`"John"`,`,`,`"lastName"`,`:`,`"Smith"`,`,`,`"age"`,`:`,`25,`,`"address"`,`:`,`{`,`"streetAddress"`,`:`,`"21 2nd Street"`,`,`,`"city"`,`:`,`"New York"`,`,`,`"state"`,`:`,`"NY"`,`,`,`"postalCode"`,`:`,`"10021-3100"`,``,`,`,`"spouse"`,`:`,`42`,``,``,`" ) $mustans = chana $mustans = ^jsonpath ( "[0`,``,``)
....,....,........................
JsonParse Call[ { "firstName
....,....,......................FAILRULE ^jsonparse(" 
[ { "firstName) => ``
....,....,....................FAILRULE ^testing() => `` 

 

 

 1 2 > 
1 of 2
 
  login or register to react