expr-json-story Pub

Json expressions

send::  msg expr.json.create  (customer="Jane"="Jane")

expect::  (cart[="id"] is "Jane")
expect::  (cart contains "Jane")

send::  msg expr.json.addItem  (sku="book1"="book1", quantity:Number="2"=2)

send::  msg expr.json.addItem  (sku="book2"="book2", quantity:Number="3"=3)

expect::  (cart is JSON)
expect::  (cart[="items"] is Array)
expect::  (cart[="items"][="1"=1] contains "book2")

Access inside JSON object using diesel expressions: send::  msg ctx.set  (size=sizeOf(x=cart[="items"]))

expect::  (size is 2)

Or using JS expression - you have more flexibility this way (costs more CPU) send::  msg ctx.set  (size74=js{{ cart.items.length }})

expect::  (size74 is 2)

You can do more fancy access: send::  msg ctx.set  (res78=js{{ cart.items[0].sku }})

expect::  (res78 is "book1")

send::  msg ctx.set  (res84:JSON=({a:"aa"} + {version:"V2"}))

expect::  (res84[="version"] == "V2")

Accessors

send::  msg ctx.set  (fieldName="items"="items")

send::  msg ctx.set  (res240=cart[="items"="items"])

expect::  (res240 is Array)
send::  msg ctx.set  (res241=cart[="items"="items"][="0"=0][:String="sku"])

expect::  (res241 is "book1")
send::  msg ctx.set  (res242=cart[=fieldName][="0"=0][:String="sku"])

expect::  (res242 is "book1")

More stuff: foreach

send::  msg ctx.foreach  (list=cart[="items"], item="item"="item", msg="testing.invcheck"="testing.invcheck")

send::  msg ctx.set  (ajson:JSON={a-num:123,bnum:321,a-string:abc})

expect::  (ajson[="bnum"] is number)
expect::  (ajson[="bnum"] is 321)
send::  msg ctx.set  (line87=js{{ ajson.bnum }})

expect::  (line87 is 321)
send::  msg ctx.set  (line89=js{{ ajson["a-num"] }})

expect::  (line89 is 123)

Array of json

they map into array of json, we can acces members or result

send::  msg ctx.set  (g45:Array=[{a:"aa"},{a:"bb"}])

send::  msg ctx.set  (g46=(g45 map ( x=>(x + {_id:x[="a"]})() )))

expect::  (g46[="0"=0][:String="_id"] is "aa")

More

Support multi-line and ID with quotes, double quotes or nothing...

send::  msg ctx.set  (ajson2:JSON={a-num:123,bnum:321,a-string:abc})

expect::  (ajson2[="bnum"] is number)
expect::  (ajson2[="bnum"] is 321)

Make sure 123 is kept as a number, not "123"

send::  msg ctx.set  (body101:JSON={accountNumber:"1729534",start_num:123})

expect::  (body101 ~= "(?s).*: 123.")

And also that we preserve the input string send::  msg ctx.set  (body110:JSON={accountNumber:"1729534",start_num:"123"})

expect::  (body110 ~= "(?s).:"123".*")

send::  msg ctx.json  (a-num:Number="123"=123, bnum:Number="321"=321, a-string=abc)

expect::  (payload[="nada"] not defined)
expect::  (payload[="bnum"] is 321)

send::  msg expr.json.create2  (a:Number="1"=1, b:Number="2"=2)

expect::  (payload[="a"] is 1)

send::  msg expr.json.sum2  (a:Number="1"=1, b:Number="2"=2)

expect::  (payload[="a"] is 1)
expect::  (payload[="b"] is 2)

More JSON examples

send::  msg ctx.echo  (msg:JSON={})

expect::  (msg is {})

send::  msg ctx.set  (msg:Array=[])

expect::  (msg is [])

send::  msg ctx.set  (msg:Array=[1,"a",3])

expect::  (msg is [1,"a",3])

send::  msg ctx.set  (a:Number="1"=1, b:Number="2"=2)

send::  msg ctx.echo  (msg:Array=[a,"a",b])

expect::  (msg is [1,"a",2])

send::  msg ctx.set  (msg127:JSON={a:"a"})

expect::  (msg127 contains ""a"")

send::  msg ctx.set  (msg130:JSON={a:[1,"a",b]})

TODO access arrays msg.a is an array

// $expect (msg130.a contains 1)

send::  msg ctx.set  (msg133:JSON={a:{a:3}})

expect::  (msg133[="a"][="a"] is 3)

send::  msg ctx.set  (msg:JSON={status:"ok",someStats:"67",accountId:a})

Numbers type

send::  msg expr.simplejson.do  (x:Number="3"=3)

expect::  (payload[="x"] is 3)
expect::  (payload[="x"] is Number)

calling a message with an object: flattening

This will match (a,b)

send::  msg ctx.json  (a:Number="1"=1, b:Number="5"=5)

send::  msg dieseltest.asAttrs  (payload.asAttrs)

expect::  (res33 is 6)

This will not match (a,b)

send::  msg ctx.json  (a:Number="7"=7)

send::  msg dieseltest.asAttrs  (payload.asAttrs)

expect::  (res33 is 6)

Complex objects

send::  msg ctx.set  (student:DieselTestStudent(JSON)={name:"John",address:{street:"Baker St",no:"121b"}})

send::  msg ctx.echo  (m=student)

send::  msg ctx.echo  (m=student[="address"][="no"])

// $send ctx.set(student.address.no="65") send::  msg test.diesel.rule40 

send::  msg ctx.echo  (m=student[="address"][="no"])

expect::  (student[="address"][="no"] is "65")

Array operators

send::  msg ctx.set  (a171:Array=[1,"a",3])

expect::  ( ("a" in [1,"a",3]))
expect::  ( ("a" in a171))
expect::  ( (3 in a171))
expect::  ( ("no" not in a171))

send::  msg test.diesel.isin  (x="123"="123", y:Number="3"=3)

expect::  (y46 is true)
send:: msg  (y46=null)
send::  msg test.diesel.isin  (x="123"="123", y:Number="1"=1)

expect::  (y46 not defined)
send:: msg  (y46=null)
send::  msg test.diesel.isin  (x="1234"="1234", y:Number="1"=1)

expect::  (y46 not defined)

Composing and subtracting from jsons:

send::  msg ctx.set  (j188:JSON={a:"a",b:"b"})

expect::  (j188[="b"] is "b")
send::  msg ctx.set  (j189=(j188 + {c:"c"}))

expect::  (j189[="c"] is "c")

send::  msg ctx.set  (j192=(j188 - {b:""}))

expect::  (j192[="b"] is undefined)

CSV/JSON data transformation

send::  msg ctx.set  (payload:Array=[{a:"aa",b:2,c:null,d:true,e:""},{a:"bb"}])

send::  msg ctx.jsonToCsv  (list=payload, separator=","=",")

expect::  (payload is Array)
send::  msg ctx.mkString  (separator=" "=" ")

expect::  (payload is String)
expect::  (payload contains "true")

send::  msg ctx.set  (csv ="llos,name,source,nlo"...="llos,name,source,nlos,timestamp,enbModel,knlos1,latlong,antennaAzimuth,lnlos1,nhlos,lhlos,earfcn,knlos2,height,enbId,lnlos2,plmnId,khlos,pci,status,nnlos1,txPower,klos,frequency,antennaGain,tm,antennaDownTilt,nnlos2,macAddress,deviceId 0,"Ilia Enb","diesel","0","2020-12-23T17:26:56.697Z","FW300i","0","42.85920, -79.24376","0","0","0","0","432155","0","85","11235813","0","00101","0","0987","ok","0","0","0","0","0","TM3","0","0",, ,"no-first-col","diesel","0","2020-12-23T17:26:56.697Z","FW300i","0","42.85920, -79.24376","0","0","0","0","432155","0","85","11235813","0","00101","0","0987","ok","0","0","0","0","0","TM3","0","0",, ")

send::  msg ctx.csvToJson  (payload=csv, separator=","=",", hasHeaders:Boolean=true)

expect::  (payload is Array)
expect::  (payload[="0"=0] is Json)

Accessor

send::  msg ctx.set  (parms="a.a.a,b.b.b"="a.a.a,b.b.b")

send::  msg ctx.set  (p0:JSON={a:{a:{a:1}},b:{b:{b:2}}})

send::  msg ctx.set  (x220=js{{ parms.split(",") }})

send::  msg ctx.echo  (x221=(x220 map ( x=>accessor(p0,accessor=x)() )))

expect::  (x221 is [1,2])

send::  msg ctx.echo  (x5=accessor(obj:JSON={a:{a:{a:1}},b:{b:{b:2}}},accessor:String="a.a.a"="a.a.a"))

access objects from/to JS

send::  msg ctx.set  (students:Array=[{name:"a",age:3},{name:"b",age:5}])

send::  msg testdiesel.jstx  (students=students)

expect::  (payload is 8)

send::  msg testdiesel.jstxo  (students=students)

expect::  (payload[="sum"] is 8)

Interpolated property names

send::  msg ctx.set  (x238:JSON={${1+2}:5})

expect::  (x238[="3"="3"] is 5)

Proper escaping of values

send::  msg ctx.set  (g45:Array=[{a:"aa"},{a:"bb"}])

send::  msg ctx.set  (g46=(g45 as string))

send::  msg ctx.set  (g47:JSON={a:("xx=" + g46)})

send::  msg ctx.echo  (x=g47[="a"])

expect::  (g47[="a"] is defined)

This one makes sure it's obtained properly unescaped:

expect::  (g47[="a"] contains ""bb"")


Was this useful?    

By: Razie | 2019-06-10 | Tags: story , dsl


Viewed 197 times ( | Print ) this page.

You need to log in to post a comment!

© Copyright DieselApps, 2012-2021, all rights reserved.