AI Zone Admin Forum Add your forum

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

CS 5.7 released-  ChatScript Meets JSON

ChatScript now supports easy access to JSON data from a website (using CURL to access). Just what you needed to respond on the fly from your ChatBot using the web. Lots of websites support JSON structured data, including IBM’s Watson, airlines, weather, etc.


  [ # 1 ]

I am trying out the new version of chatscript and can issue a ^jsonopen command and a ^jsonprint command but cannot figure out the ^jsonpath command.

I tried ^jsonpath( [0] $$jsonid )
^jsonpath( [0] $$jsonid )
^jsonpath( [0].weather.main $$jsonid )
^jsonpath( [0].main $$jsonid )
^jsonpath( [1].main $$jsonid )
^jsonpath( [1].weather.main $$jsonid )
^jsonpath( .id $$jsonid )

The last generates an error complaining that only 2 of 3 arguments were supplied.

Printing the $$jsonid and the output of the ^jsonpath of my call yields the following.  What would be the correct syntax to obtain the weather fields?  Thx.

jsonid: jo-0
"coord": {
"lon": -0.13

"lat": 51.51


“weather”: <
"id": 801

"main": Clouds

"description": few clouds

"icon": 02n





  [ # 2 ]

I expect there is an error in json path that I have fixed but not released. I will review this this weekend and release a new version as appropriate.


  [ # 3 ]

Try CS 5.72 now released.


  [ # 4 ]

Does anyone know how to compile 5.72 for the MAC?  Bruce tells me to ask here. His note about what is happening is below:

.... Message from Bruce about the Mac compile——

The mac compile command does not name the curl library so it is missing functions. I don’t know how it is supposed to be listed. I added it to the linux make file as LDFLAGS= -lrt -lcurl which for linux requires that CURL be installed as a package.  If MAC were synonymous with LINUX, then one would install curl the same way (using package install appropriate to the linux variant). But we are outside my area now, on mac. You’d need to inquire of mac people in IT or on

—end message from bruce—-

Thanks to MAC uses that know the answer!

John Bourne



  [ # 5 ]

^jsonpath is fixed now in release 5.72

I created the following sample code to test a weather api:

outputmacro: ^KelvinToFahrenheit(^value)
  $$answer = ^compute(^value - 273.15)
  $$answer = ^compute($$answer * 1.80)
  $$answer = ^compute($$answer + 32.00)
  # $$answer = ^compute($$answer mod 100)
  # $$answer = ^compute($$answer * 100)
  $$answer = ^join($$answer f)

outputmacro: ^GetLocalWeather (^Town) 
$$APIKey = 
$$url = ^join( ^Town)  #limit 1000 requests/day
$$url_apikey = ^join($$url &appid;= $$APIKey)
$$jsonid = ^jsonopen(GET $$url_apikey “” “”)

# Uncomment to troubleshoot and see json response
# ^JSONPrint($$jsonid)

$$Weather = ^jsonpath( .main jo-2)
$$WeatherDesc = ^jsonpath( .description jo-2)
$$Temp = ^jsonpath( .temp jo-3)
$$TempMin = ^jsonpath( .temp_min jo-3)
$$TempMax = ^jsonpath( .temp_max jo-3)
$$TempF = ^KelvinToFahrenheit($$Temp)
$$TempMinF = ^KelvinToFahrenheit($$TempMin)
$$TempMaxF = ^KelvinToFahrenheit($$TempMax)
You can expect $$WeatherDesc.  It is $$TempF and will range from $$TempMinF to $$TempMaxF today.

topic: ~MyWeather ( weather )

u: ( what is the weather today ) ^keep() ^repeat()
$$MyTown = Los_Angelos

Sample Output:

Welcome to ChatScript.
  >what is the weather today?
You can expect sky is clear. It is 91.06f and will range from 84.20f to 99f today.

The weather descriptions returned from the api are not consistently structured so making one generic sentence to return the weather description results does not work.  Another sample description is “scattered clouds” instead of “sky is cloudy”.  Different custom responses to the user would need to be created for each weather description returned to sound more natural.

TIP: ^jsonpath can take a first argument with spaces if you use an underscore.  In the sample above “temp min” was an object attribute name in the returned json but ^jsonpath( .temp_min jo-3) worked in referencing it and retrieving the value.

(Anyone know how to round to the nearest whole number in Chatscript?  Also, basic math was not working so had to use a series of ^compute functions for converting Kelvin to Fahrenheit.)


  [ # 6 ]

An alternative to using json path is to do a query on the field name as a verb. Typically it is unique and thus you can do this   @0 = query(direct_v ? temp_min ? 1)  @0object
This gets around the randomness of their structures.


  [ # 7 ]

Mac compilation solved. 

I found a website somewhere that said that only LINUX machines required the rt library.  So, the compilation statement given by Bruce: 

g++  -funsigned-char src/*.cpp -o chat -L/usr/lib -lrt -lcurl -lpthread 2>err.txt

is fixed for the mac by using:

g++  -funsigned-char src/*.cpp -o chat -L/usr/lib -lcurl -lpthread 2>err.txt

The code compiles fine and runs.

Now back to trying out JSON:cheese:


  [ # 8 ]

Hi, Alaric.

What seems to work for you is not responding a thing for me. I’ve no idea how else I can access the json with ^jsonpath()
I took your macros and add an app key. 

But the lines with jsonpath is leading the CS to go to the gambit rules instead.
          $$Weather = ^jsonpath( .main jo-2)

When I changed it to something like $$Weather = ^jsonpath($$jsonid[0].id.main) didn’t work as well.

Anything that I am missing here? Hope you can this thread after more than a year. Thanks.


  [ # 9 ]


I would review the latest documentation in the Json.pdf that comes with the chatscript download.  Mine is located in the following path:

For some troubleshooting suggestions see below:

Uncomment the line:
# ^JSONPrint($$jsonid)

to see the raw json response.

Remove or comment your code in your topic and just print a response to confirm your topic works.  To avoid the gambit rules you need to make sure your input includes the keywords in your topic.  Consider adding xxweather for testing and see if using xxweather in the input directs control to your topic.  You need to have the ^keep() and ^repeat() added to your match pattern or after the first response you may be directed to your gambit topics.  If you have any syntax error your topic may be skipped and you might be directed to your gambits.  if you add a
u: (*) Hi. 
to the end of your topic at least you will know that processing control reached your topic but did not match on the
u: ( what is the weather today ) ^keep() ^repeat()

If you get that far then comment out the output macro and see if that works.  Then comment the lines in the output macro and add one line at a time back until you find the line with the issue that causes the whole pattern to be skipped.

There are ways of debugging from the prompt using commands that are discussed in this document:

for instance you can force the use of a topic by using the command
:testtopic ~myweather

and you can issue the command
:trace ~myweather
to see a trace of the processing flow for that topic

Type JSON in the search box on this page and look at some other more recent examples of people using JSON with chatscript and see if there might be an easier way to test with another URL in order to rule out an issue that might be specific to this weather URL.

Let us now how your testing goes.



  [ # 10 ]

Thanks so much for the followup, Alaric. It was some problems I made hastily and it’s all working now. But the web documentation would be a big help to understand the whole thing as I am still in early stages of grasping the real power of CS behind the basics. Thanks a lot.


  [ # 11 ]

BTW, it’s now available on the ChatScript github repo a WIKI containing all documentation available:

About JSON, the doc is:




  [ # 12 ]

Hi guys. I have similar question towards reading JSON data of
Sample data i am working on :

"description":"light intensity drizzle rain",

It is no problem to view coord: lon / lat. I receive expected data. But when it comes to view: Weather: description the struggle begins.

My script:

outputmacro: ^grabheader()
  return (^
"Content-Type: application/json
  ~User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64)
  ~Cache-Control: no-cache
  ~Accept: application/json
  ~Accept-Encoding: identity,*;q=0
  ~Accept-Language: en-US,en;q=0.9"

topic: ~weatherTest1 keep()
tHI. ^keep()

t: (weather in _*>) ^keep() ^repeat() $_tmp=_0
= ^'$_cityname&APPID=e2ea5767e57c508850b26ade944068dc'
$_userAgent = ^"User-Agent: %bot, ChatScript"
$_header = ^grabheader()
$_tmp = ^jsonopen(transient get $_url NULL $_header)
$_descr = ^jsonparse(^"{ weather.decription: $_tmp }")

$ $_tmp.coord.lon is the Latitude/Longtitude of city $_cityname
The weather in $_cityname is $_descr 

Can anyone see what I am doing wrong ? I see it is something small, but cant really figure it out.

The output i receive from the script is as follows:

jack: > weather in Dublin
BOT:  53.35, -6.26 is the Latitude/Longitude of city Dublin. The weather in Dublin is jo-t1007

Thanks in advance !



  [ # 13 ]

First, $_descr = ^jsonparse(^”{ weather.decription: $_tmp }”)  has badly spelled decription field.

Second $_tmp is a json object (jo-t1007).  If you want data from it you need to retrieve it, eg $


  [ # 14 ]

$_tmp = ^jsonopen(transient get $_url NULL $_header)
$_descr = ^jsonparse(^”{ weather.decription: $_tmp }”)

First as Bruce states, ^jsonopen() returns a JSON object, something like jo-t1001.

^jsonparse() also returns a JSON object, what it does is take a string and convert it into JSON. So you are trying to parse “{ weather.description : jo-t1001 }”
That is undoubtedly not want you want and is unnecessary you already have all the JSON you need. As an aside, that string would create a simple JSON object with one property “weather.description” - again probably not what you are after.

Instead what you need:
$_descr = $[0].description
The weather property holds an array, so you want the first object in that array, and then the “description” property from that object.


  [ # 15 ]

Thank you guys. It is my fault. Misspelled the word… Sorry.
But thanks to you guys I found out how to read data located in the arrays too. Thank you Bruce & Andy !
Solved. Thanks.

Best Regards !


  login or register to react