Search code examples
javascriptchannelmirth

passing javascript object from one channel to other channel in mirth


In Channel A i am getting XML as input.Incoming XML mentioned below:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bookstore>
<books>
<book>
<title>SpringInAction</title>
<NoOfCopies>10</NoOfCopies>
</book>
<book>
<title>HibernateInAction</title>
<NoOfCopies>8</NoOfCopies>
</book>
<book>
<title>JSFInAction</title>
<NoOfCopies>5</NoOfCopies>
</book>
<book>
<title>StrutsInAction</title>
<NoOfCopies>9</NoOfCopies>
</book>
<book>
<title>JSPInAction</title>
<NoOfCopies>4</NoOfCopies>
</book>
</books>
</bookstore>

I am parsing this XML in Edit Transformer of the Source and making JavaScript object and putting this JavaScript object into a channel Map like this:

var book=new Object();
book.title = msg['books']['book'][0]['title'].toString();
book.copies = msg['books']['book'][0]['NoOfCopies'].toString();
logger.info("book "+book);
channelMap.put("book",book);

Now i want to pass this JavaScript object to other Channel B from destination of Channel A and Channel B receives this JavaScript object as input and does the rest of the processing .Is it possible in Mirth?

I am using Mirth Version 2.2.1.5861.


Solution

  • Before saving the channel variable, serialize it (json object).

    In your channel destination, write out the "book" variable.

    So something like this:

    In Transformer

    var book=new Object();
    book.title = msg['books']['book'][0]['title'].toString();
    book.copies = msg['books']['book'][0]['NoOfCopies'].toString();
    logger.info("book "+book);
    
    var Jbook = JSON.stringify(book);
    channelMap.put("Jbook",Jbook );
    

    In Destination Template

    ${jbook}
    

    Then on the receiving channel

    var book = JSON.parse(msg.toString());
    

    I have not verified this code, but it should give you a coding strategy.

    See Mirth Discussion about JSON

    Also: Please see our HealthcareIT project proposal at area51.StackExchange. This would be a good question to ask.