Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    converting multiple json to xml through vbscript

    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"
    }
    ]


  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    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:
    Code:
    <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:
    Code:
    json = _
    "{""?xml"":{""@version"":""1.0"", ""@standalone"":""no""},""root"":" &responsivePuffsContentInJson& "}"
    Try this:
    Code:
    json = _
    "{""?xml"":{""@version"":""1.0"", ""@standalone"":""no""},""root"":{""item"":" &responsivePuffsContentInJson& "}}"
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    When I did the equivalent to that in my JavaScript code, I got this result in my XML:
    Code:
    <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •