Announcement

Collapse
No announcement yet.

JSON.stringify() fails since 3.0.2

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSON.stringify() fails since 3.0.2

    Hi,

    I am working on a channel where I need to send a JSON object.
    I defined a string and now want to make it a valid JSON string with stringify. That worked in 3.0.1 but now with 3.0.2 it seems to fail.

    Transformer error
    ERROR MESSAGE: Error evaluating transformer
    com.mirth.connect.server.MirthJavascriptTransforme rException:
    CHANNEL: Listener
    CONNECTOR: Query - storePatientToDb
    SCRIPT SOURCE: TRANSFORMER
    SOURCE CODE:
    878: //WorkEmail: [email protected],
    879: //WorkFax: XXX,
    880: WorkPhone: pWorkPhone
    881: }
    882:
    883: connectorMap.put('storeToDb', JSON.stringify(storeToDb));
    884:
    885:
    886: responseMap.put('response', ResponseFactory.getSuccessResponse('<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><ActivityId CorrelationId="26f4581a-f26e-4d70-9c41-69a3a59c4732" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">95eeebe0-8bcd-4f8c-ab0a-e1b2ba45f579</ActivityId></s:Header><s:Body><SendQueryRequestResponse xmlns="http://foo.com/barConnectInterface"><SendQueryRequestResult>true</SendQueryRequestResult></SendQueryRequestResponse></s:Body></s:Envelope>'));
    887: if ('xml' === typeof msg && msg.hasSimpleContent()) { msg = msg.toXMLString(); }if ('xml' === typeof tmp && tmp.hasSimpleContent()) { tmp = tmp.toXMLString(); }
    LINE NUMBER: 883
    DETAILS: Java class "[Ljava.lang.reflect.Constructor;" has no public instance field or method named "toJSON".
    at cb5e4cfd-4f44-40ee-a480-c08bbdf130ae:883 (doTransform)
    at cb5e4cfd-4f44-40ee-a480-c08bbdf130ae:889 (doScript)
    at cb5e4cfd-4f44-40ee-a480-c08bbdf130ae:891
    at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.call(JavaScr iptFilterTransformer.java:134)
    at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.call(JavaScr iptFilterTransformer.java:100)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker( Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
    at java.lang.Thread.run(Unknown Source)

    am I missing something ..... again?

    thank you and best regards

  • #2
    What sort of object is "storeToDb"? Generally only JavaScript objects (i.e. inheriting from the Object prototype) are supported in JSON.stringify.
    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

    Nicholas Rupley
    Work: 949-237-6069
    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


    - How do I foo?
    - You just bar.

    Comment


    • #3
      Hi,

      I found out it has something to do with a certain string value.
      From HL7 U get a birthdate like 19700601

      now I use this function to convert it:
      Code:
      function _dateConvertFormat (date, formatIn, formatOut) {
      	try {
      		//importPackage(java.util)
      		importPackage(java.text)
      		//importPackage(java.sql)
      	} catch(e) {
      		//logger.error('Lib import failed: ' + e)
      		return -1
      	}
      
      	try {
      		inDate = new SimpleDateFormat(formatIn)
      	} catch(e) {
      		return -2
      	}
      
      	try {
      		outDate = new SimpleDateFormat(formatOut)
      	} catch(e) {
      		return -3
      	}
      
      	try {
      		tmpDate = inDate.parse(date)
      	} catch(e) {
      		return -4
      	}
      
      	try {
      		finalDate = outDate.format(tmpDate)
      	} catch(e) {
      		return -5
      	}
      
      	return finalDate
      
      }
      and I call it llike this
      Code:
      pDateTimeofBirth = _dateConvertFormat (msg['PID']['PID.7']['PID.7.1'].toString(), "yyyyMMdd", "yyyy-MM-dd").toString()
      if I put this variable in an object like this
      Code:
      var storeToDb = {
      	ExternalId: pID,
      	FirstName: pFirstName,
      	Gender: pSex,
      	LastName: pLastName,
      	Birthdate: pDateTimeofBirth
      }
      I'll get this error. So again, my own fault, not a bug :-\

      Anyway, every hint is appreciated

      Comment


      • #4
        SOLVED! stringify vs date strings

        I wennt again through thiss post: http://www.mirthcorp.com/community/f...ead.php?t=5128

        It seems that stringify has a problem with any date related string.
        My function also gives back a string, but somehow stringify fails on it.


        So instead of
        Code:
        pDateTimeofBirth = _dateConvertFormat (msg['PID']['PID.7']['PID.7.1'].toString(), "yyyyMMdd", "yyyy-MM-dd").toString()
        I now use
        Code:
        pDateTimeofBirth = new String(_dateConvertFormat (msg['PID']['PID.7']['PID.7.1'].toString(), "yyyyMMdd", "yyyy-MM-dd"))
        and it works.

        I hope this is helpful to someone

        Comment


        • #5
          The trick I've always used is to just append an empty JS string to the end of the mapping value, and those seem to work fine in JSON.stringify()

          Example:
          var obj = {}
          obj.firstName= msg['PID']['PID.5']['PID.5.2'].toString() + ''
          var jsonString = JSON.stringify(obj)

          Eh -- I see that was already suggested in the thread you referenced. My bad

          Comment

          Working...
          X