...

View Full Version : converting multiple json to xml through vbscript



arihan
11-23-2012, 01:34 PM
Hi,

I am facing the problem while converting json to xml through vbscript using Newtonsoft.Json.dll.

It works fine if there is single json . However its not working for multiple jsons. I've already given the sample json below and also snippet of code.

Any thoughts??????



say

============================================================
responsivePuffsContentInJson=Getxmlnodetext(XMLDoc,"/section/responsive-puffs/content")
lib =Parameter("curr_directory")&"\jsonassembly\Bin\Net35\Newtonsoft.Json.dll"
Set JsonConvert = DOTNetFactory.CreateInstance("Newtonsoft.Json.JsonConvert", lib)
json = "{ ""?xml"": { ""@version"": ""1.0"", ""@standalone"": ""no"" }, ""root"":" &responsivePuffsContentInJson& "}"

Set xmlDoc = JsonConvert.DeserializeXmlNode(json)

Dim xmlDocc,convcontent

Set xmlDocc = CreateObject("Msxml2.DOMDocument")

xmlDocc.async = False

convcontent=xmlDoc.InnerXml

msgbox "convcontent"&convcontent

xmlDocc.loadXML(convcontent )

=======================================

working fine for json


[
{
"id": "47638",
"sectionColour": "#0a5f00",
"headline": "arihan new puff 20th nov",
"author": "ariahn test author",
"teaser": "test ",
"components": {},
"publishDate": "2012-11-20T17:49:15Z",
"idLink": "6313_6316_122_47594",
"puffType": "times_cutOut"
}
]

doesnot work for below json

[
{
"id": "41942",
"sectionColour": "#8c8c3f",
"headline": "F1 Puff",
"author": "Bob James",
"teaser": "For all the latest Formula One News",
"components": {
"images": [
{
"url": "http://tablet-si.cloud-newsint.co.uk/times/incoming/article48913.ece/BINARY/10inch_p/CAROL_MIDGLEY.png",
"width": 300,
"height": 499,
"crop": true,
"mandatory": false,
"deviceType": "images",
"addToCarousel": false,
"disableTap": false
}
]
},
"publishDate": "2012-11-05T11:53:42Z",
"idLink": "7019_7022_122_41430"
},
{
"id": "47953",
"sectionColour": "#8c8c3f",
"headline": "arihan test puff 21st nov -1 headline",
"author": "test",
"teaser": "test",
"publishDate": "2012-11-21T11:24:53Z",
"webLink": "http://www.google.com",
"puffType": "times_cutOut"
},

{
"id": "48905",
"sectionColour": "#8c8c3f",
"headline": "Puff Headline09:45:42",
"author": "SmokeTest Automation Author",
"teaser": "SmokeTest Automation Teaser",
"publishDate": "2012-11-22T09:45:44Z",
"puffType": "times_cutOut"
}
]

Old Pedant
11-23-2012, 11:24 PM
Is there some reason you chose to use VBScript for this instead of JavaScript?

After all JSON is an acronym for "JavaScriptObjectNotation" so it is not surprising that other languages would struggle with it.

Clearly, that Newtonsoft.Json.dll you say you are using is simply inadequate. There is probably not much point in pursuing this until/unless you can get whoever created that DLL to fix it.

Also, you are *NOT* using "multiple JSON" there.

The code you showed in bold is one single JSON encoding. There is nothing "multiple" about it. Ttue, it is an array of 3 objects, with sub-objects in some cases. But the first code you show is also an array. It just happens to have only one element in the array instead of 3 and it has no sub-objects.

If all you are trying to do is convert that JSON to XML, it would be nearly trivial to do so using JavaScript instead of that hacked up VBScript that has to invoke some .NET created DLL.

Old Pedant
11-24-2012, 12:08 AM
Okay, so I decided to test myself. I said writing a JSON to XML converter in JavaScript is nearly trivial, so I decided to try it.

And it was.

BUT...

When I converted the JSON you show there to XML look what I got:


<root>
<id>41942</id>
<sectionColour>#8c8c3f</sectionColour>
<headline>F1 Puff</headline>
<author>Bob James</author>
<teaser>For all the latest Formula One News</teaser>
<components>
<images>
<url>http://tablet-si.cloud-newsint.co.uk/times/incoming/article48913.ece/BINARY/10inch_p/CAROL_MIDGLEY.png</url>
<width>300</width>
<height>499</height>
<crop>true</crop>
<mandatory>false</mandatory>
<deviceType>images</deviceType>
<addToCarousel>false</addToCarousel>
<disableTap>false</disableTap>
</images>
</components>
<publishDate>2012-11-05T11:53:42Z</publishDate>
<idLink>7019_7022_122_41430</idLink>
</root>
<root>
<id>47953</id>
<sectionColour>#8c8c3f</sectionColour>
<headline>arihan test puff 21st nov -1 headline</headline>
<author>test</author>
<teaser>test</teaser>
<publishDate>2012-11-21T11:24:53Z</publishDate>
<webLink>http://www.google.com</webLink>
<puffType>times_cutOut</puffType>
</root>
<root>
<id>48905</id>
<sectionColour>#8c8c3f</sectionColour>
<headline>Puff Headline09:45:42</headline>
<author>SmokeTest Automation Author</author>
<teaser>SmokeTest Automation Teaser</teaser>
<publishDate>2012-11-22T09:45:44Z</publishDate>
<puffType>times_cutOut</puffType>
</root>

You see the problem? AS GIVEN, you would need to REPEAT the <root> element!!!

So I *THINK* then answer is simple.

Instead of just using:


json = _
"{""?xml"":{""@version"":""1.0"", ""@standalone"":""no""},""root"":" &responsivePuffsContentInJson& "}"

Try this:


json = _
"{""?xml"":{""@version"":""1.0"", ""@standalone"":""no""},""root"":{""item"":" &responsivePuffsContentInJson& "}}"

Old Pedant
11-24-2012, 12:13 AM
When I did the equivalent to that in my JavaScript code, I got this result in my XML:


<root>
<item>
<id>41942</id>
<sectionColour>#8c8c3f</sectionColour>
<headline>F1 Puff</headline>
<author>Bob James</author>
<teaser>For all the latest Formula One News</teaser>
<components>
<images>
<url>http://tablet-si.cloud-newsint.co.uk/times/incoming/article48913.ece/BINARY/10inch_p/CAROL_MIDGLEY.png</url>
<width>300</width>
<height>499</height>
<crop>true</crop>
<mandatory>false</mandatory>
<deviceType>images</deviceType>
<addToCarousel>false</addToCarousel>
<disableTap>false</disableTap>
</images>
</components>
<publishDate>2012-11-05T11:53:42Z</publishDate>
<idLink>7019_7022_122_41430</idLink>
</item>
<item>
<id>47953</id>
<sectionColour>#8c8c3f</sectionColour>
<headline>arihan test puff 21st nov -1 headline</headline>
<author>test</author>
<teaser>test</teaser>
<publishDate>2012-11-21T11:24:53Z</publishDate>
<webLink>http://www.google.com</webLink>
<puffType>times_cutOut</puffType>
</item>
<item>
<id>48905</id>
<sectionColour>#8c8c3f</sectionColour>
<headline>Puff Headline09:45:42</headline>
<author>SmokeTest Automation Author</author>
<teaser>SmokeTest Automation Teaser</teaser>
<publishDate>2012-11-22T09:45:44Z</publishDate>
<puffType>times_cutOut</puffType>
</item>
</root>


But I still have to ask why you chose to do this in ugly VBScript instead of making it *SO* much simpler if you had just use JavaScript.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum