Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: Convert Json Data to XML CreateJsonReader Serialize Deserialize System.Net System.IO.StreamReader System.Net.WebRequest System.Runtime.Serialization System.Xml.Linq System.Xml System.Runtime.Serialization.Json.JsonReaderWriterFactory System.Xml.XmlDictionaryReaderQuotas System.Text.Encoding System.Xml.Linq.XElement System.Xml.XmlDocument
;*************************************************************************** ;** Convert Json Data to XML ;** ;** Purpose: Convert Json Data to XML ;** Inputs: url that contains json data ;** Outputs: Results in an XML File ;** Reference: ;** REQUIRES WinBatch 2013A or newer ;** ;** Developer: Deana Falk 2013.08.07 ;*************************************************************************** If Version( ) < '2013A' Pause( 'Notice', 'Need 2013A or Newer Version of WinBatch' ) Exit EndIf ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Inputs ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ url = 'http://openlibrary.org/api/books?bibkeys=ISBN:0451526538&jscmd=details' outputfile = DirScript():'Output.XML' ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Load assembly into the WinBatch process ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ObjectClrOption( 'use', 'System.Net, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ) ObjectClrOption( 'use', 'System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' ) ObjectClrOption( 'use', 'System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' ) ObjectClrOption( 'use', 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' ) ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Get Json ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Web = ObjectClrNew( 'System.Net.WebRequest' ) Web1 = Web.Create( url ) oTxt = Web1.GetResponse( ) cStream = oTxt.GetResponseStream( ) StreamReader = ObjectClrNew( 'System.IO.StreamReader', cStream ) strTxt = StreamReader.ReadToEnd( ) Message( 'Original JSON', strTxt ) ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; JSON data returned from URL ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;var _OLBookInfo = {"ISBN:0451526538": {"info_url": "http://openlibrary.org/books/OL1017798M/The_adventures_of_Tom_Sawyer", "bib_key": "ISBN:0451526538", "preview_url": "http://openlibrary.org/books/OL1017798M/The_adventures_of_Tom_Sawyer", "thumbnail_url": "http://covers.openlibrary.org/b/id/295577-S.jpg", "details": {"identifiers": {"goodreads": ["1929684"], "project_gutenberg": ["74"], "librarything": ["2236"]}, "subject_place": ["Mississippi River Valley", "Missouri"], "covers": [295577], "lc_classifications": ["PS1306 .A1 1997"], "latest_revision": 8, "genres": ["Fiction."], "source_records": ["marc:marc_records_scriblio_net/part25.dat:213801824:997", "marc:marc_loc_updates/v40.i23.records.utf8:2463307:1113"], "title": "The adventures of Tom Sawyer", "languages": [{"key": "/languages/eng"}], "subjects": ["Sawyer, Tom (Fictitious character) -- Fiction", "Runaway children -- Fiction", "Child witnesses -- Fiction", "Boys -- Fiction", "Mississippi River Valley -- Fiction", "Missouri -- Fiction"], "publish_country": "nyu", "by_statement": "Mark Twain ; with an introduction by Robert S. Tilton.", "oclc_numbers": ["36792831"], "type": {"key": "/type/edition"}, "revision": 8, "publishers": ["Signet Classic"], "last_modified": {"type": "/type/datetime", "value": "2012-06-13T23:35:32.882463"}, "key": "/books/OL1017798M", "authors": [{"name": "Mark Twain", "key": "/authors/OL18319A"}], "publish_places": ["New York"], "pagination": "xxi, 216 p. ;", "classifications": {}, "created": {"type": "/type/datetime", "value": "2008-04-01T03:28:50.625462"}, "lccn": ["96072233"], "notes": "Includes bibliographical references (p. 213-216).", "number_of_pages": 216, "dewey_decimal_class": ["813/.4"], "isbn_10": ["0451526538"], "publish_date": "1997", "works": [{"key": "/works/OL53919W"}]}, "preview": "noview"}};Start = StrIndex( cTxt, '{', 1, @Fwdscan )-1 ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Parse Json Data ; Remove leading variable 'var _OLBookInfo = ' and ending semi-colon ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ startptr = Strindex( strTxt, '{', 1, @Fwdscan ) endptr = StrLen( strTxt ) - startptr strJson = StrSub( strTxt, startptr, endptr ) Message( 'Revised JSON', strJson ) ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Convert to XML ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ JsonRWFactory = ObjectClrNew( 'System.Runtime.Serialization.Json.JsonReaderWriterFactory' ) XmlDictionaryReaderQuotas = ObjectClrNew( 'System.Xml.XmlDictionaryReaderQuotas' ) Encoding = ObjectClrNew( 'System.Text.Encoding' ) XElement = ObjectClrNew( 'System.Xml.Linq.XElement' ) XmlDocument = ObjectClrNew( 'System.Xml.XmlDocument' ) ByteArrayJson = Encoding.UTF8.GetBytes(strjson) ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; CreateJsonReader ; Creates An XmlDictionaryReader that can process JavaScript Object Notation (JSON) data. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ reader = JsonRWFactory.CreateJsonReader( ByteArrayJson, XmlDictionaryReaderQuotas.Max ) Xml = XElement.Load( reader ) XmlDocument.LoadXml( Xml.ToString( ) ) XmlDocument.Save( outputfile ) Run( outputfile, '' )
Article ID: W17850
Filename: Convert Json To XML.txt
File Created: 2013:08:07:13:28:58
Last Updated: 2013:08:07:13:28:58