Search This Blog

Showing posts with label api. Show all posts
Showing posts with label api. Show all posts

Tuesday, 7 May 2013

XML2JSON with javascript

This post is more or less a code snippet how to convert a XML-Content into JSON with javascript.

I had a problem with a very big XML-Entity and got an error
"errorcode":"steps.xml2json.ExecutionFailed"
-> this is my solution with a javascript-policy !

Here you can see it for the request flow. Change all "request"-vars to "response" and you are ready for the response flow. 

As you can see https://gist.github.com/olegp/642667 in comments section I had to modify it a little bit, because CDATA-Tags where not parsed correct - E4X has some common problems with CDATA and I was happy to found a working solution.
-> https://developer.mozilla.org/de/docs/E4X/Processing_XML_with_E4X
...For backwards compatibility, E4X defaults to ignoring comments and CDATA sections...

Tip: If you want to modify the result look at the ignore list and play around => ["type", "space", "xmlns", "html"]

// found here https://gist.github.com/olegp/642667
function E4XtoJSON(xml, ignored) {
  var r, children = xml.*, attributes = xml.@*, length = children.length();
  if(length == 0) {
    r = xml.toString();
  } else if(length == 1) {
    var text = xml.toString();
    if(text) {
      r = text;
    }
  }
  if(r == undefined) { 
    r = {};
    for each (var child in children) {
     var name = child.localName();
     var json = E4XtoJSON(child, ignored);
     var value = r[name];
     if(value) {
       if(value.length) {
         value.push(json);
       } else {
         r[name] = [value, json]
       }
     } else {
       r[name] = json;
     }
    }
  }
  if(attributes.length()) {
    var a = {}, c = 0;
    for each (var attribute in attributes) {
      var name = attribute.localName();
      if(ignored && ignored.indexOf(name) == -1) {
        a["_" + name] = attribute.toString();
        c ++;
      }
    }
    if(c) {
      if(r) a._ = r;
      return a;
    }
  }

  return r;
};

var oldcontent=request.body.asXML;
var newcontent=JSON.stringify(E4XtoJSON(oldcontent, ["type", "space", "xmlns", "html"]), null, '  ');

request.headers['Content-Type'] = 'application/json';
request.body = newcontent;

Why I write about apigee?

First of all - I have no relation to apigee and just using the free plan.

I will write how to manage things and how to solve api-needs you could have and maybe you can save many hours or days or even month to do it by your own.


Everything you need is to get a free accout here: http://apigee.com/about/ (Enterprise)

Start looking this yt-video from Greg Brail to understand what a big pleasure apigee's api gives to you.


In a way I am a apigee beginner with a lot of time pressure to solve a project. More than a year ago I saw a new addon on heroku.com and played a little bit with apigee-console. In a way it was boring so I dropped it very fast and forgot it.

About 8 weeks ago I got a nice project with a requirement to connect one app with several api's (and in future more api's) and googled around and found apigee again. (and remembered my boring time a year ago)

Looking two days at the documentation I was very depressed, because I was not able to understand how to use apigee. Terrible!

Now - about 4 weeks later - I know more and I think I am able to convert nearly every api to what I want! Amazing!

I wanted to start writing this blog with the finished project, but perhaps it's a good idea to do it now. And you can following my learning curve.....and I have to finish the project in the next 3 weeks.

First I will show you some basic tricks and then I plan to do a real world project with you -> to convert a complete soap-xml-rpc api into a json rest api !!!