Feature Request - Support for json transformations via liquid

bbhatia
Participant III

It will be nice to have liquid support in apigee for json transformations which will be similar to xslt for xml to xml.

2 3 355
3 REPLIES 3

Nice. Good request.

I understand that you'd like an easy way to transform json to json . I agree that would be a nice built-in feature to have.

Have you considered accomplishing what you want using the extensibility in Apigee to accomplish what you want? For example you could use Handlebars within a JS Callout. The policy configuration might look like this:

<Javascript name='JS-Handlebars-1' timeLimit='200' >
  <Properties>
    <Property name='model'>myMessage.content</Property>

    <!-- you could assign this template to a variable with AssignMessage.
         or just embed the template right here. -->
    <Property name='template'>TEMPLATE_HERE</Property>
    <Property name='output'>view</Property>
  </Properties>

  <!-- obtain the handlebars source from
       https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.3.1/handlebars.js -->

  <IncludeURL>jsc://handlebars.js</IncludeURL>
  <ResourceURL>jsc://applyHandlebarsTemplate-1.js</ResourceURL>
</Javascript>

The "applyHandlebarsTemplate-1.js" is boilerplate. It looks like this:

var model    = JSON.parse(context.getVariable(properties.model));
var template = Handlebars.compile(String(properties.template));
context.setVariable(properties.output, template(model));

Here is an API Proxy that uses handlebars within the JS callout.

apiproxy-handlebars-example.zip

In this example, the template looks like this:

{
  "cities" : [
    {{#each cities}}
    {
      "id" : "{{id}}",
      "name" : "{{name}}",
      "currentConditions" : {
        {{#main}}
        "currentTemp" : "{{temp}}",
        "humidity" : "{{humidity}}",
        {{/main}}
        "windSpeed" : "{{wind.speed}}",
        {{#weather.[0]}}
        "summary" : "{{main}}",
        "description" : "{{description}}"
        {{/weather.[0]}}
      }
    }{{#unless @last}},{{/unless}}
    {{/each}}
  ]
}

The source data is formatted like this:

{
    "cities": [
        {
            "coord": {
                "lon": -0.13,
                "lat": 51.51
            },
            "weather": [
                {
                    "id": 300,
                    "main": "Drizzle",
                    "description": "light intensity drizzle",
                    "icon": "09d"
                }
            ],
            "base": "stations",
            "main": {
                "temp": 280.32,
                "pressure": 1012,
                "humidity": 81,
                "temp_min": 279.15,
                "temp_max": 281.15
            },
            "visibility": 10000,
            "wind": {
                "speed": 4.1,
                "deg": 80
            },
            "clouds": {
                "all": 90
            },
            "dt": 1485789600,
            "sys": {
                "type": 1,
                "id": 5091,
                "message": 0.0103,
                "country": "GB",
                "sunrise": 1485762037,
                "sunset": 1485794875
            },
            "id": 2643743,
            "name": "London",
            "cod": 200
        },...

And the output is:

{
  "cities" : [
    {
      "id" : "2643743",
      "name" : "London",
      "currentConditions" : {
        "currentTemp" : "280.32",
        "humidity" : "81",
        "windSpeed" : "4.1",
        "summary" : "Drizzle",
        "description" : "light intensity drizzle"
      }
    },
    {
      "id" : "2172797",
      "name" : "Cairns",
      "currentConditions" : {
        "currentTemp" : "300.15",
        "humidity" : "74",
        "windSpeed" : "3.6",
        "summary" : "Clouds",
        "description" : "scattered clouds"
      }
    }
  ]
}

Interesting use of handlebars for json mapping. As apigee already support xslt using saxon engine and i guess saxon now support xslt 3.0 which also can be used for json--> json. Refer https://www.saxonica.com/papers/xmlprague-2016mhk.pdf. Wouldn't that be easier to upgrade xslt policy to support xslt 3.0 rather than creating new policy from scratch.

Yes it would be easier in theory. But then you still have to write XSLT. !!!!

¯\_(ツ)_/¯