web stats
JSON.stringify() fails since 3.0.2 - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 04-24-2014, 02:18 AM
gkittlaus gkittlaus is offline
OBX.3 Kenobi
 
Join Date: Jan 2012
Posts: 104
gkittlaus is on a distinguished road
Default 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.

Quote:
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: xxx@example.com,
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
Reply With Quote
  #2  
Old 04-24-2014, 07:37 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,126
narupley is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 04-24-2014, 08:25 AM
gkittlaus gkittlaus is offline
OBX.3 Kenobi
 
Join Date: Jan 2012
Posts: 104
gkittlaus is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 04-25-2014, 02:15 AM
gkittlaus gkittlaus is offline
OBX.3 Kenobi
 
Join Date: Jan 2012
Posts: 104
gkittlaus is on a distinguished road
Default 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
Reply With Quote
  #5  
Old 04-25-2014, 02:08 PM
joshillian joshillian is offline
Mirth Newb
 
Join Date: May 2010
Location: Temple, TX
Posts: 9
joshillian is on a distinguished road
Default

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
Reply With Quote
Reply

Tags
json, stringify

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 04:23 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation