Yes. Using the JsonConvert class which contains helper methods for this precise purpose:
// To convert an XML node contained in string xml into a JSON string
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
// To convert JSON text contained in string json into an XML node
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
Documentation here: Converting between JSON and XML with Json.NET
Answer from David Brown on Stack OverflowYes. Using the JsonConvert class which contains helper methods for this precise purpose:
// To convert an XML node contained in string xml into a JSON string
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
// To convert JSON text contained in string json into an XML node
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
Documentation here: Converting between JSON and XML with Json.NET
Yes, you can do it (I do) but Be aware of some paradoxes when converting, and handle appropriately. You cannot automatically conform to all interface possibilities, and there is limited built-in support in controlling the conversion- many JSON structures and values cannot automatically be converted both ways. Keep in mind I am using the default settings with Newtonsoft JSON library and MS XML library, so your mileage may vary:
XML -> JSON
- All data becomes string data (for example you will always get "false" not false or "0" not 0) Obviously JavaScript treats these differently in certain cases.
- Children elements can become nested-object
{}OR nested-array[ {} {} ...]depending if there is only one or more than one XML child-element. You would consume these two differently in JavaScript, etc. Different examples of XML conforming to the same schema can produce actually different JSON structures this way. You can add the attribute json:Array='true' to your element to workaround this in some (but not necessarily all) cases. - Your XML must be fairly well-formed, I have noticed it doesn't need to perfectly conform to W3C standard, but 1. you must have a root element and 2. you cannot start element names with numbers are two of the enforced XML standards I have found when using Newtonsoft and MS libraries.
- In older versions, Blank elements do not convert to JSON. They are ignored. A blank element does not become "element":null
A new update changes how null can be handled (Thanks to Jon Story for pointing it out): https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_NullValueHandling.htm
JSON -> XML
- You need a top level object that will convert to a root XML element or the parser will fail.
- Your object names cannot start with a number, as they cannot be converted to elements (XML is technically even more strict than this) but I can 'get away' with breaking some of the other element naming rules.
Please feel free to mention any other issues you have noticed, I have developed my own custom routines for preparing and cleaning the strings as I convert back and forth. Your situation may or may not call for prep/cleanup. As StaxMan mentions, your situation may actually require that you convert between objects...this could entail appropriate interfaces and a bunch of case statements/etc to handle the caveats I mention above.
Transforming XML to JSON in C++ - Software Engineering Stack Exchange
How to convert XML to JSON in C++? - Stack Overflow
JSON XML in C/C++ - Stack Overflow
[C#] How to properly convert this XML into valid JSON?
Videos
By far, the only specifically designed C++ library that directly converts XML to JSON I found on the Internet is xml2json: https://github.com/Cheedoong/xml2json
Please head to http://json.org. You may find something of interest there. It lists many libraries available for JSON processing in C++ and many other languages.
But for the thing you linked to [pdhcc] you may have to do some work yourself to port it to C++.
By far, the only specifically designed C++ library that directly converts XML to JSON I found on the Internet is xml2json: https://github.com/Cheedoong/xml2json
You can also convert JSON to XML if following the same rules.
Boost.PropertyTree handles both JSON and XML. There are some quirks in their implementations, so it wouldn't be a direct transformation, but it shouldn't need much work to adapt a property_tree between JSON and XML.
So I am using a third party API which is a SOAP server sending response strings that imitate a JSON structure, this is the return response I get from the server which is a list of addresses.
<return xsi:type="xsd:string">{"res":"
[]","res_addr":"
[{\"id\":\"A_02774200\",\"t\":\"\\\"\\u0100r
ai\\u0161u stacija\\\" - 1, Drabe\\u0161u pag.,
C\\u0113su nov., LV-4101\"},
{\"id\":\"A_02774201\",\"t\":\"\\\"\\u0100ra
i\\u0161u stacija\\\" - 2, Drabe\\u0161u pag.,
C\\u0113su nov., LV-4101\"},
{\"id\":\"A_02774202\",\"t\":\"\\\"\\u0100ra
i\\u0161u stacija\\\" - 3, Drabe\\u0161u pag.,
C\\u0113su nov., LV-4101\"}, ...How do I properly convert the unicode letters and get rid of those backslashes " things to get a valid JSON?
Maybe this is a bad, dirty approach to the problem but what I did so far was use XDocument.Parse to get it to look like this
<return xsi:type="xsd:string">{"res":"[]","res_addr":"
[{\"id\":\"A_02774200\",\"t\":\"\\\"\\u0100rai\\u0161u stacija\\\" - 1, Drabe\\u0161u pag., C\\u0113su nov., LV-4101\"},
{\"id\":\"A_02774201\",\"t\":\"\\\"\\u0100rai\\u0161u stacija\\\" - 2, Drabe\\u0161u pag., C\\u0113su nov., LV-4101\"},
{\"id\":\"A_02774202\",\"t\":\"\\\"\\u0100rai\\u0161u stacija\\\" - 3, Drabe\\u0161u pag., C\\u0113su nov., LV-4101\"},...Then I used (string)doc.Descendants("return").Single() to get rid of the XML tags and I used the Regex.Unescape() command 2 times in a row
After the first one:
{"res":"[]","res_addr":"[{"id":"A_02774200","t":"\"\u0100rai\u0161u
stacija\" - 1, Drabe\u0161u pag., C\u0113su nov., LV-4101"},
{"id":"A_02774201","t":"\"\u0100rai\u0161u stacija\" - 2, Drabe\u0161u pag., C\u0113su nov., LV-4101"},
{"id":"A_02774202","t":"\"\u0100rai\u0161u stacija\" - 3, Drabe\u0161u pag., C\u0113su nov., LV-4101"},
{"id":"A_02774203","t":"\"\u0100rai\u0161u stacija\" - 4, Drabe\u0161u pag., C\u0113su nov., LV-4101"},..After the second one:
{"res":"[]","res_addr":"[{"id":"A_02774200","t":""Āraišu stacija" - 1, Drabešu pag.,
Cēsu nov., LV-4101"},{"id":"A_02774201","t":""Āraišu
stacija" - 2, Drabešu pag., Cēsu nov., LV-4101"},
{"id":"A_02774202","t":""Āraišu stacija" - 3, Drabešu pag., Cēsu nov., LV-4101"},{"id":"A_02774203","t":""Āraišu stacija" - 4, Drabešu pag.,
Cēsu nov., LV-4101"},{"id":"A_00420070","t":""Āraišu stacija" - 5,
Drabešu pag., Cēsu nov., LV-4101"},I also use .Replace("\"[","["); and .Replace("]\"", "]"); to remove the quotes next to [ and ] which also made the JSON invalid.
But I have run into a problem with some of the items where they have double quotes ("") inside of the t field containing double quotes which make it invalid JSON like this:
https://postimg.cc/w7zMLLNf
Is there maybe a string manipulation technique I could use to fix cases like this and to achieve a valid JSON or do I have to somehow convert the XML into JSON in a better way from the start not using the Regex.Unesacpe and string manipulations I did before?