TCL Json builder

Version 1
    This document was generated from CDN thread

    Created by: Grant Bagdasarian on 15-02-2012 10:31:19 AM
    Hello,
     
    I'm writing a TCL application which has to place a call and store some information like the outcome, duration etc. The data it gathers needs to be sent over HTTP to a handler written in C#. It performs a handoff to a VXML when the destination answers and this VXML also returns information back. My handler accepts only Json strings to deserialize the Json strings into objects.
     
    At this point I'm doing something like this:
     

    proc build_callsession_json_string {startTime endTime} {
    set json { 
      {
       "StartTime": $startTime,
       "EndTime": $endTime
      }
    }
    return [subst $json]
    }

     
    When I send this to my HTTP handler the string is not encoded correctly, and so the string is not escaped properly.
    How do I solve this? Are there perhaps easier ways to create Json strings.
     
    Thanks,

    Grant

    Subject: RE: TCL Json builder
    Replied by: Anusha Kannappan on 16-02-2012 01:55:27 AM
    Hi Grant,

    For variable substitution to happen we need to enclose them within double quotes rather than flower brackets.

    Here is a sample for the same
    set a 10
    set b "{\"b\":\"$a\",\"c\":\"$a\"}"

    output:
    --------
    {"b":"10","c":"10"}

    Hope it is useful.

    Thanks,
    Anusha

    Subject: RE: TCL Json builder
    Replied by: Grant Bagdasarian on 16-02-2012 02:12:13 AM
    Yes, that is very helpful. Thank you!

    I'm going to try it out.

    Subject: RE: TCL Json builder
    Replied by: Grant Bagdasarian on 16-02-2012 02:28:14 AM
    I have another Json string which is returned by a VXML. This string is not escaped like the way you showed me. For VXML this works perfectly and I do not need to escape the Json string when I use the cisco-data element. The namelist is automatically escaped.

    How do I escape the characters in an already defined Json string in TCL without having to go through each character in the string and escaping them? Is there perhaps an easier way to do this? If not, then I'm either forced to escape the whole string or handoff all information I gather in the TCL to another VXML and have that handle my logging. I'd like to keep it in TCL, but if there is no other way, then so be it.

    Subject: RE: TCL Json builder
    Replied by: Anusha Kannappan on 16-02-2012 02:40:17 AM
    Hi Grant,

    In TCL there is no other go, for escaping the special character we need to go through each character in the string. Could you please briefly explain your scenario with a sample example so that it will be easy for us to understand and provide you a solution.

    Thanks,
    Anusha

    Subject: RE: TCL Json builder
    Replied by: Grant Bagdasarian on 16-02-2012 04:39:39 AM
    There are a few components in our scenario:
    1) A TCL script which runs on our voice gateway and periodically polls for outbound calls to place. It uses HTTP to talk to a Windows Service.
    2) A Windows Service which listens for HTTP requests and responds back with a possible string. The string format is -> Destination|Service.
    3) A Windows Service which handles Json strings and stores the information in a database.

    When the TCL places the outbound call and the destination answers an handoff to a VXML application is done. This VXML application is the one in the "Service" returned by the Windows Service. This VXML performs different things, as we have different applications. In the VXML some values are stored, like DTMF capture, transfer results etc. Once the call has ended all information is gathered and converted to Json strings. The VXML still does the converting to Json, but not the calling of another HTTP handler to send all the information.

    Instead I want the VXML to return all its created Json strings to the TCL and have the TCL handle the calling of another HTTP handler. The TCL also has to gather his own information, like outcome and duration of the initial call which it placed. I kind of wish to centralize all the information gathering and then send them all to another Windows Service which is responsible for handling these Json strings and logging them in a database.

    The problem now is that TCL can't easily handle those Json strings, since I have  to escape all the charachters.

    Below a sample of a Json string created by the handoff VXML:

    {Status: "Connected", Supplier: "1", Customer: "1", CallGuid: "EEBE2393-62C7-4E37-98EE-76C31B207B21", Destination: "1001", Source: "1000", StartTime: "2012-02-10 09:27:08.732", EndTime: "2012-02-10 09:27:13.181"}

    The initial call placed by the TCL is also a BCallLeg(This is a term defined for outgoing calls by our company). So the initial call information also has to be converted to a Json string and both need to be added to an array in Json format.

    For this I created the below proc. It still needs to be modified to escape everything properly and add the [ ] to signify an array. The problem lies with the $bcallleg variable. Since this is the Json string from above.

    proc build_bcalllegs_json_string {bcallleg startTime endTime source destination callguid customer supplier status} {
    set json {
      {
       "BCallLegs": {
        "StartTime": $startTime,
        "EndTime": $endTime,
        "Source": $source,
        "Destination": $destination,
        "CallGuid": $callguid,
        "Customer": $customer,
        "Supplier": $supplier,
        "Status": $status
       },
        $bcallleg
      }
    }
    return [subst $json]
    }

    Subject: RE: TCL Json builder
    Replied by: Anusha Kannappan on 16-02-2012 05:44:18 AM
    Hi Grant,

    As far as my knowledge we surely have to make use of the '\' for escaping the special characters. And if you really find it very difficult to do that it is better to pass the information you gather in TCL to VXML and from there you could do a handoff to the Windows service.

    Thanks,
    Anusha

    Subject: RE: TCL Json builder
    Replied by: Grant Bagdasarian on 16-02-2012 05:57:06 AM
    Alright. I think that's the best way to do so, since VXML can handle these things easily.

    Thank you for your help!