Jump to content

Basic JSON to XML converter


drlava
 Share

Recommended Posts

A while ago I ran across some JSON formatted data from a Microsoft website, and being without a JSON UDF like this nice XML one, I decided that converting the file to XML would be a good way to easily access data within it. (using the XML UDF after conversion)

the JSON format may be slightly nonstandard, and went like this:

JavaFunc(
{"FisrtNodes":
[
{"FirstNodeCode":432,"SubNodes":
[
{"AttribTxt":"lol","AttribNumeric":28455699,"AttribDateCode":Date.UTC(2007,6,9,18,0,0,0),"AttribFinalNumeric":60
},
{"AttribTxt":"whimper","AttribNumeric":09841,"AttribDateCode":Date.UTC(1970,1,1,1,0,0,0),"AttribFinalNumeric":69
},
{"AttribTxt":"Hey, You There!","AttribNumeric":0,"AttribDateCode":Date.UTC(1999,12,30,23,0,0,0),"AttribFinalNumeric":13455
}
]
},
{"FirstNodeCode":0976,"SubNodes":
[
{"AttribTxt":"lol","AttribNumeric":28455699,"AttribDateCode":Date.UTC(2007,6,9,18,0,0,0),"AttribFinalNumeric":60
},
{"AttribTxt":"whimper","AttribNumeric":09841,"AttribDateCode":Date.UTC(1970,1,1,1,0,0,0),"AttribFinalNumeric":69
},
{"AttribTxt":"Hey, You There!","AttribNumeric":0,"AttribDateCode":Date.UTC(1999,12,30,23,0,0,0),"AttribFinalNumeric":13455
}
]
}
],"PlainNode":
{"AttribID":"This.Is.The.ID","AttribEmptyText":"","AttribFriendlyName":null,"AttribFinalNumeric":1380
},"PlainNode2":
{"AttribID":"This.Is.The.ID","AttribEmptyText":"","AttribFriendlyName":null,"AttribFinalNumeric":1380
}
})

except without any @CR or @NL chars (added for readability).

This is a re-entrant converting function with nested self-referencing regex, and it currently doesn't handle spaces in the structure or anything but attribute data. But it is a decent starting point, because it handles unlimited nesting depths and sizes. Because the XML write functions write to the HDD every time, large conversions take some time. I found that storing the XML file in a temporary ramdisk helped, but with reasonably-sized XML files this should be no problem. If you improve or add more JSON format capabilities, please verify with the included JSON2XMLExample.au3, add your own JSON example file and reference result to the zip,(in addition to the included) and submit it here!

Thanks for your comments and enjoy. This was my first real autoit3 function.

Attachment contains:

- _JSONWrapper.au3 UDF

- _XMLDomWrapper.au3 UDF (modified from here to handle multiple files at once)

- ExampleJSON.txt file containing code to be converted

- JSON2XMLExample.au3 Example usage and compatibility verifier for your additions

- ReferenceResult.xml Reference XML output for compatibility verification.

JSON2XML.zip

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...