CL 5.8 – JSON format

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf CL 5.8 – JSON format

  • Creator
    Topic
  • #54619
    Rob Lindsey
    Participant

      Anyone ever tried to deal with JSON formatted data messages either with TCL or Xlates.  I know with TCL there is a way to do it with a proc if you have TCL 8.5 but not with 8.4.  

      Yes I know that I can upgrade and get TCL 8.5 but that is a few months off at the earliest and of course they want this data from JSON to HL7 2.3.1 sooner rather than later.  I am sure that I can figure out how to do it but I thought I would reach out to the community and see if anyone has done it in the past.

      TIA

      Rob

    Viewing 6 reply threads
    • Author
      Replies
      • #82242
        Rob Abbott
        Keymaster

          There are style sheets available that will convert JSONXML.  You could then deal with the data as XML inside Cloverleaf.

          Rob Abbott
          Cloverleaf Emeritus

        • #82243
          Levy Lazarre
          Participant

            Another alternative is to use the json package that is part of Tcllib. It includes a parser for JSON. Very easy to use.

            However, the JSON text is converted to a Tcl dictionary. Therefore, if you are on Tcl 8.4, you will also have to download and install the “dict” package for 8.4, besides Tcllib.

            package require json

            Then use the command “::json::json2dict” to parse your JSON text into a Tcl dict.

            Example from the command line:

            Code:



            % package require json
            1.1.2
            % set jsontxt {[
               {
                   “id”: 2,
                   “name”: “An ice sculpture”,
                   “price”: 12.50,
                   “tags”: [”cold”, “ice”],
                   “dimensions”: {
                       “length”: 7.0,
                       “width”: 12.0,
                       “height”: 9.5
                   },
                   “warehouseLocation”: {
                       “latitude”: -78.75,
                       “longitude”: 20.4
                   }
               },
               {
                   “id”: 3,
                   “name”: “A blue mouse”,
                   “price”: 25.50,
                   “dimensions”: {
                       “length”: 3.1,
                       “width”: 1.0,
                       “height”: 1.0
                   },
                   “warehouseLocation”: {
                       “latitude”: 54.4,
                       “longitude”: -32.7
                   }
               }
            ]

            }

            % ::json::json2dict $jsontxt

            {id 2 name {An ice sculpture} price 12.50 tags {cold ice} dimensions {length 7.0 width 12.0 height 9.5} warehouseLocation {latitude -78.75 longitude 20.4}} {id 3 name {A blue mouse} price 25.50 dimensions {length 3.1 width 1.0 height 1.0} warehouseLocation {latitude 54.4 longitude -32.7}}

            Once your JSON is parsed into a dictionary, you use the regular dict commands to retrieve the desired data elements.

            I hope this helps.

          • #82244
            Jim Kosloskey
            Participant

              My reading on this topic – and I admit I just started to pay attention to it – indicates both the json package and using stylesheets have drawbacks primarilly due to the apparent ‘on the fly’ nature of JSON.

              This appears to be expressed in the literature as ambiguity.

              My take away is there is no real standard and no real ‘defintion’ of the message  other than what one finds actually in the message.

              So perhaps the handling of the messages could break easily.

              Looks fast to setup and perhaps a pain to maintain and administer.

              Again some fairly uneducated thoughts on my part.

              email: jim.kosloskey@jim-kosloskey.com 30+ years Cloverleaf, 60 years IT – old fart.

            • #82245
              Levy Lazarre
              Participant

                This may have been true in the past, but the JSON format has been rigorously defined since 2013 by the ECMA-404 international standard. There are literally dozens of libraries in Java, JavaScript, Ruby, Perl, Groovy, and many other languages that allow to parse and serialize JSON.

                http://www.json.org

                Similarly to XML, one can define schemas for documents

                http://www.json-schema.org

                In fact, the format is so well established that the new HL7 standard (FHIR) offers two data interchange formats (XML and JSON), with JSON being favored because it’s a lot more efficient and less verbose than XML.

              • #82246
                Jim Kosloskey
                Participant

                  Good information.

                  This will help focus my research – thanks Levy.

                  email: jim.kosloskey@jim-kosloskey.com 30+ years Cloverleaf, 60 years IT – old fart.

                • #82247
                  Rob Lindsey
                  Participant

                    Thanks all.

                    I will let you know which route will be taken.

                    Rob

                  • #82248
                    Keith McLeod
                    Participant

                      Was there ever a resolve in using JSON with a REST webservice in cloverleaf?  I am being required to receive JSON and map to xml on the client side.

                  Viewing 6 reply threads
                  • The forum ‘Cloverleaf’ is closed to new topics and replies.