You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can also add a flag safe to `^jsonparse`, `^jsonobjectinsert`, `^jsonarraydelete`.
85
-
You can also add a flag unique to `^jsonarrayinsert`.
86
-
You can also add `DUPLICATE` to ^jsonobjectinsert.
83
+
You can also add a flag `safe` to `^jsonparse`, `^jsonobjectinsert`, `^jsonarraydelete`.
84
+
You can also add a flag `unique` to `^jsonarrayinsert`.
85
+
You can also add `duplicate` to ^jsonobjectinsert.
87
86
88
87
When multiple flags are desired, put them into a simple string, `"DUPLICATE PERMANENT"`. Case doesn't matter.
89
88
90
89
When you want to add a reference to a normal factid (as is returned by ^createfact) you can add the flag `AUTODELETE`
91
90
92
-
You can also assign user flags by listing USER_FLAG1, thru USER_FLAG4 as a flag as well. The JSON fact will have that flag on it, which you can use in conjunction with ^query to limit what matches can be found.
91
+
You can also assign user flags by listing `USER_FLAG1`, through `USER_FLAG4` as a flag as well.
92
+
The JSON fact will have that flag on it, which you can use in conjunction with `^query` to limit what matches can be found.
93
+
93
94
94
95
### `^jsonparse`( {JSONFLAGS} string )
96
+
95
97
`string` is a JSON text string (as might be returned from a website) and this parses into facts.
96
98
It returns the name of the root node JSON composite. This name will look like this:
97
99
98
-
`ja-0` – a json array numbered `0`.
99
-
100
-
`jo-0` – a json object numbered `0`.
100
+
*`ja-0` – a json array numbered `0`.
101
+
*`jo-0` – a json object numbered `0`.
101
102
102
103
As new JSON composites are created during a volley, the numbers increase to keep them all distinct.
103
104
JSON composites are all created as transient facts and will die at the end of the volley unless you do
@@ -177,7 +178,9 @@ nominally, because it is considered a single token.
177
178
You can bypass this limit by asking the tokenizer to directly process OOB data, returning the JSON structure name instead of all the content. Just enable `#JSON_DIRECT_FROM_OOB` on the `$cs_token` value and if it finds OOB data that is entirely JSON, it will parse it and return something like `jo-t1` or `ja-t1` in its place. Eg.
178
179
`[ { "key": "value} ]` will return tokenized as `[jo-t1]`.
179
180
180
-
##jsonformat(string)`
181
+
182
+
## `jsonformat`(string)
183
+
181
184
Because technically JSON requires you put quotes around field names
182
185
(though various places ignore that requirement) and because CS doesn't, the function takes in a slack
183
186
json text string and outputs a strict one.
@@ -186,12 +189,14 @@ json text string and outputs a strict one.
186
189
## Accessing JSON structures
187
190
188
191
### `^jsonpath`( string id )
189
-
`string` is a description of how to walk JSON.
190
-
`id` is the name of the node you want to start at (typically returned from `^jsonopen` or `^jsonparse`).
192
+
193
+
*`string` is a description of how to walk JSON.
194
+
*`id` is the name of the node you want to start at (typically returned from `^jsonopen` or `^jsonparse`).
191
195
192
196
Array values are accessed using typical array notation like `[3]` and object fields using dotted notation.
193
197
A simple path access might look like this: `[1].id` which means take the root object passed as id,
194
198
e.g., `ja-1`, get the 2nd index value (arrays are 0-based in JSON).
199
+
195
200
That value is expected to be an object, so return the value corresponding to the id field of that object.
196
201
In more complex situations, the value of id might itself be an object or an array,
197
202
which you could continue indexing like `[1].id.firstname`.
@@ -227,7 +232,8 @@ if you add a 3rd argument "safe" to the call.
227
232
```
228
233
229
234
### `^jsonpath`
230
-
can also return the actual factid of the match, instead of the object of the fact. This would
235
+
236
+
Can also return the actual factid of the match, instead of the object of the fact. This would
231
237
allow you to see the index of a found array element, or the json object/array name involved. Or you
232
238
could use ^revisefact to change the specific value of that fact (not creating a new fact). Just add * after
233
239
your final path, eg
@@ -259,6 +265,7 @@ You may omit the leading . of a path and CS will by default assume it
259
265
If a variable holds a JSON object value, you can directly set and get from fields of that object
260
266
using dotted notation. This can be a fixed static fieldname you give or a variable value:
261
267
`$myvar.$myfield` is legal.
268
+
262
269
Dotted notation is cleaner and faster than `^jsonpath` and `jsonobjectinsert` and for get, has
263
270
the advantage that it never fails, it only returns null if it can't find the field.
264
271
On the other hand, assignment fails if the path does not contain a json object at some level.
@@ -329,23 +336,28 @@ number restricts how deep it displays. 0 (default) means all. 1 is just top leve
329
336
You can build up a JSON structure without using `^jsonparse` if you want to build it piece by piece.
330
337
And you can edit existing structures.
331
338
339
+
332
340
### `^jsoncreate`( {JSONFLAGS} type )
341
+
333
342
Type is either array or object and a json composite with no content is created and its name returned.
334
343
See `^jsonarrayinsert`, `^jsonobjectinsert`, and `^jsondelete` for how to manipulate it.
335
344
See writeup earlier about optional json flags.
336
345
346
+
337
347
### `^jsonarrayinsert`( {JSONFLAGS} arrayname value )
348
+
338
349
Given the name of a json array and a value, it adds the value to the end of the array.
339
350
See writeup earlier about optional json flags.
340
351
If you use the flag unique then if value already exists in the array, no duplicate will be added.
341
352
342
-
### `^jsonarraydelete`( [INDEX, VALUE] arrayname value )
353
+
354
+
### `^jsonarraydelete`( [`INDEX`, `VALUE`] arrayname value )
355
+
343
356
This deletes a single entry from a JSON array. It does not damage the thing deleted, just its member in the array.
344
-
If the first argument is INDEX, then value is a number which is the array index (0 … n-1).
345
-
If the first argument is VALUE, then value is the value to find and remove as the object of the json fact.
357
+
*If the first argument is `INDEX`, then value is a number which is the array index (0 ... n-1).
358
+
*If the first argument is `VALUE`, then value is the value to find and remove as the object of the json fact.
346
359
347
-
You can delete every matching `VALUE` entry by adding the optional argument `ALL`. Like:
348
-
`^jsonarraydelete("INDEX ALL" $array 4)`
360
+
You can delete every matching `VALUE` entry by adding the optional argument `ALL`. Like: `^jsonarraydelete("INDEX ALL" $array 4)`
349
361
350
362
If there are numbered elements after this one, then those elements immediately renumber downwards
351
363
so that the array indexing range is contiguous.
@@ -356,12 +368,17 @@ suppress this with the `SAFE` flag. `^jsonarraydelete(SAFE $obj $key)`.
356
368
357
369
358
370
### `^jsonarraysize`( name )
371
+
359
372
deprecated in favor of ^length
360
373
374
+
361
375
### `^jsoncopy`( name )
376
+
362
377
Given the name of a json structure, makes a duplicate of it. If it is not the name of a json structure, it merely returns what you pass it.
363
378
379
+
364
380
### `^jsonobjectinsert`( {JSONFLAGS} objectname key value )
381
+
365
382
inserts the key value pair into the object named. The key does not require quoting.
366
383
Inserting a json string as value requires a quoted string.
367
384
Duplicate keys are ignored unless the optional 1st argument `DUPLICATE` is given.
@@ -371,30 +388,40 @@ If the key has an existing value and `DUPLICATE` is not a factor, then if the va
371
388
recursively deleted provided its data is not referenced by some other fact (not by any variables). You can
372
389
suppress this with the `SAFE` flag. `jsonobjectinsert(SAFE $obj $key null)`.
373
390
391
+
374
392
### `^jsondelete`( factid )
393
+
375
394
deprecated in favor of ^delete
376
395
396
+
377
397
### `^jsongather`( {fact-set} jsonid {level})
398
+
378
399
takes the facts involved in the json data (as returned by `^jsonparse` or `^jsonopen`)
379
-
and stores them in the named factset.
400
+
and stores them in the named factset.
380
401
This allows you to remove their transient flags or save them in the users permanent data file.
381
402
382
403
You can omit fact-set as an argument if you are using an assignment statement:
383
-
`@1 = ^jsongather(jsonid)'
404
+
`@1 = ^jsongather(jsonid)`
384
405
385
406
`^Jsongather` normally gathers all levels of the data recursively. You can limit how far down it goes by
386
407
supplying `level`. Level 0 is all. Level 1 is the top level of data. Etc.
387
408
409
+
388
410
### `^jsonlabel`( label )
411
+
389
412
assigns a text sequence to add to jo- and ja- items created thereafter. See System functions manual.
390
413
414
+
391
415
### `^jsonundecodestring`( string )
416
+
392
417
removes all json escape markers back to normal for possible printout
393
418
to a user. This translates \\n to newline, \\r to carriage return, \\t to tab, and \\" to a simple quote.
@@ -544,11 +574,63 @@ Note in this next example how to escpe a json string with ^''. This makes creat
544
574
^jsontree( $$tmp ) \n
545
575
name: $$tmp.name, phone: $$tmp.phone
546
576
547
-
This example shows the . notation access of data inside an json object in chatscript. This is probably the most intuitive way of interacting with the data.
577
+
This example shows the . notation access of data inside an json object in chatscript. This is probably the most intuitive way of interacting with the data.
548
578
549
579
u: (-testcase3) $$tmp = ^jsoncreate(object)
550
580
$$tmp.name = "Todd Kuebler"
551
581
$$tmp.phone = "555-1212"
552
582
^jsonwrite( $$tmp ) \n
553
583
^jsontree( $$tmp ) \n
554
-
name: $$tmp.name, phone: $$tmp.phone
584
+
name: $$tmp.name, phone: $$tmp.phone
585
+
586
+
587
+
### Arrays of objects
588
+
589
+
In the example below, we add two items into an array of objects and we display the formatted array:
590
+
591
+
```
592
+
u: ( testcase4 )
593
+
# create a phoneBook as an array of structured items (objects)
0 commit comments