AI Zone Admin Forum Add your forum

Chatbot Conference London On Oct 3th Erwin will be moderating on the Chatbots conference in London. Why don't you meet him there? As a Chatbots.org visitor, you'll get a discount as well: Why don't we meet there? get a discount

We do experience some trouble with our forms and some details page. We'll fix this ASAP. Sorry for the inconvenience!
Problem parsing JSON with two array objects
 
 

Hi there all.
First Chascript is fantastic.
I am trying to develop an app that comunicate with CS via OOB messages. especially JSONs.

Here is my MAJOR problem.

I have a question regarding JSON parsing

so i got this json for example:

{
  
"first-array"[
    {
      
"fname""John",
      
"sname""Doe"
    
},
    
{
      
"fname""Mary",
      
"sname""Jane"
    
}
  ]
,
  
"second-array"[
    {
      
"fname""John2",
      
"sname""Doe2"
    
},
    
{
      
"fname""Mary2",
      
"sname""Jane2"
    
}
  ]

Notice there are two objects “first-array” and “second-array”

when i use this in ChatScript

t: ( \[ _\]) ^keep() ^repeat() 
  
$_incoming_json = ^jsonparse(safe _0)
  ^
jsontree($_incoming_json

i got this output:

MYBOT:  JSON=> 
{    # jo-t0
  
"first-array"[    # ja-t1000
    
{    # jo-t1001
      
"fname""John",
      
"sname""Doe"
    
},
    
{    # jo-t1002
      
"fname""Mary",
      
"sname""Jane"
    
}
  ]

<=JSON

You notice that object “second-array” from the original JSON is not present on the output.
I am missing something? i’ve searched the internet but i couldn’t find an answer.

More. if i use a JSON like this:

{
  
"a""value",
  
"b"[
    {
      
"fname""John",
      
"sname""Doe"
    
},
    
{
      
"fname""Mary",
      
"sname""Jane"
    
}
  ]
,
  
"c"[
    {
      
"fname""John2",
      
"sname""Doe2"
    
},
    
{
      
"fname""Mary2",
      
"sname""Jane2"
    
}
  ]
,
  
"d""value",
  
"e""value"

after output, anything that is after the first array is gone.So the second arrsy and the other two objects after.

like this:

MYBOT:  JS
{    # jo-tchatscript-output0
  
"a""value",
  
"b"[    # ja-tchatscript-output1000
    
{    # jo-tchatscript-output1001
      
"fname""John",
      
"sname""Doe"
    
},
    
{    # jo-tchatscript-output1002
      
"fname""Mary",
      
"sname""Jane"
    
}
  ]
}
<=JSON 

I guess every time ^jsonparse() encounter a JSON array the parsing is somehow stopped.
even if i feed tit with only an array and inside are other arrays after parsing i have only the first array from the root array.

Can you help please?

here is the mac compiled version info:

ChatScript EVSERVER Version 7.5 pid59653 64 bit MACH compiled Jun 19 2017 19:23:07 host=local
Params
:   dict:2097151 fact:800000 text:100000kb hash:215127 
          buffer
:80x80kb cache:1x5000kb userfacts:100 outputlimit:80000 loglimit:80000
WordNet
dict=200486  fact=85532  heap=13570880 May31'17-15:51:39
Build0:  dict=68133  fact=129260  heap=1149344 Compiled:Sep06'
17-15:38:06 by version 7.5 "0"
Build1:  dict=196  fact=269  heap=173452 Compiled:Sep12'17-14:01:32 by version 7.5 "glas"
Used 58MB: dict 268,815 (21505kb) hashdepths 74287.68627.39257.18976.8363.3519.1354.496.165.65.15.1.2.1. words 268815 unusedbuckets 74287 fact 215,061 (10322kb) heap 14893kb
           buffer (6400kb) cache (5000kb)
Free 113MB: dict 1,828,335 fact 584,939 stack/heap 85,106KB 
 

 
  [ # 1 ]

Bogdan, we do this all the time.

First off, do you have #JSON_DIRECT_FROM_OOB in your $cs_token? This allows the tokenizer to automatically parse JSON in the OOB. The net result is that your OOB then looks like [ jo-t0 ] and with a rule like ( < [ _*1 ] ) then _0 will contain that JSON reference and you can proceed directly from there.

With respect to your issue, you rule is only matching a single array, so that it is why only the first one is parsed.

Generally OOB messages are enclosed in [ ], are you doing this?

 

 
  [ # 2 ]

Hi . thank you for your response.
I have to say that im using Chatscript for over a year now and i’v read all the manuals over and over.
To answer your question about #JSON_DIRECT_FROM_OOB…yes. i’v already tried that with no success.

I already feed Chatscript with my OOB messaged using [] squarebrackets.

What i need to acomplish is this
I need to parse a JSON but only on a specific rule so i developed some rules in my topics that look like this:

#![ response-myapp {"a":"value", "b":"value", "c":["a","b"],"d":["a",b"]} ]
u: (< \[ * response-myapp _* \] )

....and here i am parsing the JSON from _0 and compose specific messages for this rule and whatever the JSON contains

In order to send a specific JSON only to a rule i have to differentiate so i used these “labels” like the one you see above “response-myapp” wich could be quite long (as an example: framework-response-book-meeting resolved {JSON} ).

The example used in my first post was only an example. im not using anywhere the rule
u: (< \[ _* ] )

So what is wrong? I have to use _*1 in the rule?

#![ response-myapp {"a":"value", "b":"value", "c":["a","b"],"d":["a",b"]} ]
u: (< \[ * response-myapp _*1 \] )

I just tried that .. no match.

I dont really understand why my rule is matching only an array.
Can you please try to reproduce this?
Is there another way to acomplish this with some other method?

Thank you for your support.

 

 
  [ # 3 ]

you cant do this: t: ( \[ _* \])
because it will stop memorizing at the first ] it finds in the middle of your json.
You can do this
t: ( \[ _*1 \]  if your bot definition sets #JSON_DIRECT_FROM_OOB

one might also do t: (\[ _*) and use your safe convert, but then you cant accept any user input as well.
Also json size limitations will be 254 tokens in input. DIRECT_FROM_OOB has no limit

 

 
  [ # 4 ]

Ah..now i get it.. thank you very very much.. it was the “]” from the array ... ok ..thank you again.

 

 
  login or register to react