Tuesday, March 31, 2015

AX2012 JSON 2 XML converter


While writing some code for the Exchange Rate Provider class, I ran across an issue where some exchange rate provider APIs only gave JSON output instead of XML.  I found very little in Dynamics AX that would let me deserialize JSON.  The only alternative was to manually parse the data based on the structure provided at the time.

I didn't like that approach.  Manually parsing data like that can be a nightmare.  The only tools I know of in Dynamics AX are the XML tools.  With that in mind, I decided to write a JSON to XML converter.

classdeclaration


loadFromFile


convert2xml

convert

getTag




Sample JSON exchange rate data from Fixer.io.



XML version after running it through the converter.


4 comments:

Unknown said...

Hi,

Do you know how to convert from xml to json using x++ code?

Regards
Melvin

Maksym Neskoromnyi said...

Hello Joe, very lot thank you!

Anibal Bravo said...

Thank you

Regards

amoxto

Unknown said...

Thanks a lot for the script,
but probably it contains error, maybe it must be like this:
for(i=1;i<=strLen(_str);i++)
{
if (subStr(_str,i,1) == '"')
{
inquote= !inquote;
}
if (char2num(_str,i) <= #SPACE && !inquote)
{
continue;
}
tmpStr = tmpStr + subStr(_str,i,1);
}
return tmpStr;
}

Otherwise, it cust all SPACE characters from string including those in quotes.