This project has moved and is read-only. For the latest updates, please go here.

Prepend root to Json for xml?

Jan 5, 2009 at 8:59 PM
Im just trying to use the crunchbase api (

Is there any easy way to prepend a root to their json so I can deserializexmlnode without much hassle?  I just want to use xpath to get the info quick and dirty but it dies.

I tried:
            strJSon = @"{""crunchbase"":" + strJSon + "}"; //[/end ugly hack]
            XmlDocument newDoc = (XmlDocument)JsonConvert.DeserializeXmlNode(strJSon);

Unfortunately, it gives me a Newtonsoft.Json.JsonSerializationException: Unexpected JsonToken: StartArray

Any good way to handle json that doesnt have a root and just give a generic one for simplicity rather than fumble around with messing with the json string myself?
Jan 5, 2009 at 9:15 PM
I believe there is something to do with complexity of the json as a small one like this:

will work.  Perhaps the overview attribute has some crazy characters causing the parser to puke?
Jan 6, 2009 at 9:37 AM
I have added an overload to DeserializeNode which you can specify a root element to be added. It is checked into the source on CodePlex.
Jan 6, 2009 at 2:20 PM
Edited Jan 6, 2009 at 2:35 PM
Thanks James!  Very nice addition to the xmlnode converter.  An issue I have found and don't know why is for this URL

Any idea why it pukes at line 484 when it gets to the bolded line:

"image":  {"available_sizes":    [[[113,       150],      "assets/images/resized/0001/1070/11070v1-max-150x150.jpg"],     [[189,       250],      "assets/images/resized/0001/1070/11070v1-max-250x250.jpg"],     [[341,       450],      "assets/images/resized/0001/1070/11070v1-max-450x450.jpg"]],   "attribution": "\u003Cp\u003EScott Beale / \u003Ca href=\"\" title=\"Laughing Squid\" rel=\"nofollow\"\u003ELaughing Squid\u003C/a\u003E\u003C/p\u003E"},

After further investigation, it seems to not expect a new array in XmlNodeConverter.cs on around like 372.  It is only expecting a propertyname or an endobject but not a StartArray (designated by the "[" character after the available_sizes attribute).

Im not familiar enough with your source to know what needs to be done to fix that.  Any ideas?
Jan 7, 2009 at 2:11 AM
The problem is arrays nested directly inside arrays. That wasn't something I had considered when I wrote the converter. I'm not sure how you would represent that in XML.
Jan 7, 2009 at 2:16 PM
Then im guessing this library wont work at all even for just deserializing it to an object as it will ignore this special array sequence?

Could you write it out as this and call it a stringed collection when deserialized?  I suppose a person will need to know if a certain element will have an unnamed numbered array but it's one way...
<0><![CDATA[113, 150, "assets/images/resized/0001/1070/11070v1-max-150x150.jpg"]]></0>
<1><![CDATA[189, 250, "assets/images/resized/0001/1070/11070v1-max-250x250.jpg"]]></1>
<2><![CDATA[341, 450, "assets/images/resized/0001/1070/11070v1-max-450x450.jpg"]]></2>
Jan 9, 2009 at 3:00 PM
Have you thought about this problem at all James?  I can imagine you are pretty busy but was hoping you could squeeze little old me and my problem in.  :D
Jan 11, 2009 at 9:04 AM
I have made it so that it repeats the last property name, in this case available_sizes, as the element name for each nested array level.

The update is checked into CodePlex.
Jan 12, 2009 at 6:45 PM
That makes you the man.  Thanks!