web stats
delete of JSON msg object results in null - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 01-17-2020, 07:58 AM
mdehoog mdehoog is offline
Mirth Guru
 
Join Date: Mar 2011
Location: Apeldoorn the Netherlands
Posts: 301
mdehoog is on a distinguished road
Send a message via Skype™ to mdehoog
Default delete of JSON msg object results in null

I use this to remove certain object from a JSON message received.
Code:
for (i=0; i < msg.results.length; i++) {
   if(msg.results[i].PMT != '1'){
   	delete msg.results[i];
   }  
}
It does indeed remove the object but it leave a null,in the message like this:
Code:
		null,
		null,
		null,
		null,
		null,
		null,
How to remove these nulls also?
__________________
X Connections
https://documentor.email
https://www.x-connections.com
Reply With Quote
  #2  
Old 01-17-2020, 08:42 AM
mdehoog mdehoog is offline
Mirth Guru
 
Join Date: Mar 2011
Location: Apeldoorn the Netherlands
Posts: 301
mdehoog is on a distinguished road
Send a message via Skype™ to mdehoog
Default Solved it. Well kind of

Don't know if it is the best solution, so any improvement is welcome.
I found it is not ok to use delete on arrays.

Quote:
Delete works a little differently than you may expect with arrays, and shouldn't really be used. For one, delete doesn't affect the length of the array. For two, it leaves this undefined value in the middle of the array, which means you'll have to deal with it in code using the array.
In summary, don't use delete on arrays.
This does the job.
var j=msg.results.length;
for (i=0; i < j; i++) {
if(msg.results[i].PMT != '1'){
msg['results'].splice(i, 1);
j=msg.results.length;
i=i-1;
}
}

splice does it.
__________________
X Connections
https://documentor.email
https://www.x-connections.com
Reply With Quote
  #3  
Old 01-17-2020, 11:23 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,095
agermano is on a distinguished road
Default

I had a similar issue earlier this week where I needed to delete from a javascript array and shift the remaining items to fill in the gaps. I almost switched to using a java linked list instead to make it easier, but ended up finding splice and basically implemented it the same way.

Code:
for (var i = 0; i < msg.results.length;) {
    if (msg.results[i].PMT != '1')
        msg.results.splice(i, 1);
    else
        i++;
}
Reply With Quote
  #4  
Old 01-20-2020, 05:04 AM
mdehoog mdehoog is offline
Mirth Guru
 
Join Date: Mar 2011
Location: Apeldoorn the Netherlands
Posts: 301
mdehoog is on a distinguished road
Send a message via Skype™ to mdehoog
Default beter code

Your code is indeed better. Nice improvement.
__________________
X Connections
https://documentor.email
https://www.x-connections.com
Reply With Quote
Reply

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 08:25 AM.


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