drivefast/asterisk-res_json

Would it be very difficult to have bidimensional arrays ?

digas opened this issue · 6 comments

digas commented

Helllo, i really appreciate your module , and i'm a 0 on C coding. I have a situation where the json i have to construct as a second level :
jsonStar{"eventTypeId":2,"eventDate":1439304404,"msisdnPartyA":"940000001","msisdnPartyB":"940000002","contentTo":["contentType":2,"contentId":3983636,"timePlayed":7],"keyPressed":"'*'","enumEntryPoint":18,"platformId":1}

Would it be very complicate or impossible to have name in the array ?
Thanks

I may not understand your questions correctly, but you can, in fact, obtain the Nth element of an array, no matter how many levels down the array is. I think the problem you're having is that your example is not a valid JSON string. Specifically, the 'contentTo' element has the syntax of an array, but inside you try to express the values like in a dictionary. I think you want to use curly brackets instead of square brackets.

Cheers.

digas commented

You are correct , i have manually recreated what i wanted it to look like , to be a valid on it should be like this :
{"eventTypeId":2,"eventDate":1439304404,"msisdnPartyA":"940000001","msisdnPartyB":"940000002","contentTo":[{"contentType":2,"contentId":3983636,"timePlayed":7}],"keyPressed":"'*'","enumEntryPoint":18,"platformId":1}

But my problem is the array (as you mention in the jsonAdd [Arguments]):
"_name
name of element to be added; if adding to an array element, the new
element is appended and the name is ignored
_"

And in my test it was true i needed names on the array like : {key:value,key:value}
and it only returns the values in the array, no keys...

I had this lines in my context:
same => n,jsonadd(jsonStar,,number,eventTypeId,2)
same => n,jsonadd(jsonStar,,number,eventDate,${UNIQUEID:0:10})
same => n,jsonadd(jsonStar,,string,msisdnPartyA,${CALLERID(num)})
same => n,jsonadd(jsonStar,,string,msisdnPartyB,${EXTEN})
; here is where the Array starts
same => n(contentTo),jsonadd(jsonStar,,array,contentTo)
same => n,jsonadd(jsonStar,/contentTo,string,,'contentType:2')
same => n,jsonadd(jsonStar,/contentTo,string,,'contentId:3983636')
same => n,jsonadd(jsonStar,/contentTo,string,,'timePlayed:${CDR(duration)}')

same => n,jsonadd(jsonStar,,string,keyPressed,'*')
same => n,jsonadd(jsonStar,,number,enumEntryPoint,18)
same => n,jsonadd(jsonStar,,number,platformId,1)
same => n,set(response=${CURL(http://myserver:8081/event/addEvent,${jsonStar})})
same => n,DumpChan()
same => n,Hangup()

So my question was is there a way to have a "key:value" array there ?
thanks

I think what you're calling a "key-value array" is what i call a "dictionary", and what you would ultimately like to have is:
{
"eventTypeId":2,
"eventDate":1439304404,
"msisdnPartyA":"940000001",
"msisdnPartyB":"940000002",
"contentTo: {
"contentType":2,
"contentId":3983636,
"timePlayed":7
},
"keyPressed":"'*'",
"enumEntryPoint":18,
"platformId":1
}
Feel free to correct me if my assumption is wrong. But if i guessed right, did you try adding the 'contentTo' element as a "node" (instead of "array"), and continue filling that way?

same => n(contentTo),jsonadd(jsonStar,,node,contentTo)
same => n,jsonadd(jsonStar,/contentTo,string,contentType,2)

digas commented

THANKS !!!!!!!!!!!! sorry i was really focus on the array problem thanks .... i will try it know.
Thanks a lot ! ;)
(should i do anything else to close this issue or you take care of it ...)

Please confirm once it worked for you and i will close.

Cheers.

digas commented

Yes it work as you told , thanks again. Really sorry. Next time i will read the README till the end ;)

jsonStar={"eventTypeId":2,"eventDate":1439329000,"msisdnPartyA":"940000001","msisdnPartyB":"940000002","contentTo":{"contentType":2,"contentId":3983636,"timePlayed":11},"keyPressed":"*","enumEntryPoint":18,"platformId":1}