web stats
Parse JSON data with duplicate Key values - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 07-11-2016, 05:11 AM
vibinChander vibinChander is offline
OBX.2 Kenobi
 
Join Date: Jul 2015
Location: Chennai
Posts: 74
vibinChander is on a distinguished road
Send a message via Skype™ to vibinChander
Default Parse JSON data with duplicate Key values

Hi Guys,

Im reading a JSON data and converting into another format. I have a small problem. I cannot read json key with duplicate entries. I have attached my json data here.

Code:
"sourceLevelInfo_2000A": [{
		
		"informationSource_2000A":{
		"idNumber": "1",
		"levelCode": "20",
		"childCode": "1"
		},
		"sourceName_2100A": {
			"eIdCode": "PR",
			"eTypeQualifier": "2",
			"orgName": "TEST DATA",
			"name_first": "",
			"name_middle": "",
			"name_suffix": "",
			"idQualifier": "46",
			"idCode": "TEST"
		},
		"informationSource_2000A":{
		"idNumber": "9",
		"levelCode": "55",
		"childCode": "85"
		},
		"sourceName_2100A": {
			"eIdCode": "PR",
			"eTypeQualifier": "2",
			"orgName": "TEST INT",
			"name_first": "",
			"name_middle": "",
			"name_suffix": "",
			"idQualifier": "46",
			"idCode": "XXXX"
		}
	}]
In the above code "informationSource_2000A" and "sourceName_2100A" are repeated twice (It may repeat n number of times). My code is here as follows.

Code:
var HLlength = msg[0]['sourceLevelInfo_2000A'].length;
var countHL = 0;
var countNM1 = 0;
var dataHL = {};
for(i=0;i<HLlength;i++)
{
     dataHL.idNumber =msg[0]['sourceLevelInfo_2000A'][i]['idNumber'];
     dataHL.levelCode =msg[0]['sourceLevelInfo_2000A'][i]['levelCode'];
     dataHL.childCode =msg[0]['sourceLevelInfo_2000A'][i]['childCode'];
	var xmlHL_2000A = new XML ("<HL></HL>");
	//HL
	xmlHL_2000A['HL'];
	xmlHL_2000A['HL']['HL.01']=dataHL.idNumber;
	xmlHL_2000A['HL']['HL.02']=dataHL.levelCode;
	xmlHL_2000A['HL']['HL.03']=dataHL.childCode;
	countHL++;
	globCount++;
     //NM1
	var content_2000A = header+ SerializerFactory.getSerializer('EDI/X12').fromXML(xmlHL_2000A);
}
The problem i face is , mirth reads the last value as the only value available in source transformer. When duplicate key appears, it reads only one time that too the last value/.....

Kindly help to me solve this issue.
__________________

Mirth Interface Engineer
AWS DevOps
Reply With Quote
  #2  
Old 07-11-2016, 07:27 AM
callmeonnet callmeonnet is offline
Mirth Newb
 
Join Date: Apr 2015
Posts: 17
callmeonnet is on a distinguished road
Default

Hello Vibin,

Your json data is invalid.

Json's key name cannot be same within a same parent hierarchy.

Refer below example

Code:
[{

	"informationSource_2000A": {
		"idNumber": "1",
		"levelCode": "20",
		"childCode": "1"
	},
	"sourceName_2100A": {
		"eIdCode": "PR",
		"eTypeQualifier": "2",
		"orgName": "TEST DATA",
		"name_first": "",
		"name_middle": "",
		"name_suffix": "",
		"idQualifier": "46",
		"idCode": "TEST"
	}
}, {
	"informationSource_2000A": {
		"idNumber": "9",
		"levelCode": "55",
		"childCode": "85"
	},
	"sourceName_2100A": {
		"eIdCode": "PR",
		"eTypeQualifier": "2",
		"orgName": "TEST INT",
		"name_first": "",
		"name_middle": "",
		"name_suffix": "",
		"idQualifier": "46",
		"idCode": "XXXX"
	}
}]
-
Arvind Balachandran
Reply With Quote
  #3  
Old 07-11-2016, 09:37 AM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 832
siddharth is on a distinguished road
Default

The JSON is perfectly valid, it validates in a JSON parser.
Reply With Quote
Reply

Tags
edi x12, json, mirth3.3.1

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 12:16 PM.


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