05-22-2017 07:06 AM
Hello All,
I am on designing part of my application in which i need to convert the json to XML and XML to Json.
Is there any toolkits available for such conversion?
Any suggestion on how to enable this conversion Mechanism with formatting Functions.
I found a dll Names newtonsoft.dll
XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string jsonText = JsonConvert.SerializeXmlNode(doc);
but there is no public containers availabel in LabVIEW for JsonConvert Function.
Please provide the idea on how to proceed.
05-22-2017 07:16 AM
What version of LV do you use?
There are "Flatten To" and "Unflatten From" functions for XML and JSON available in LV (at least 2016).
05-22-2017 07:56 AM
Currently using LV2015
05-22-2017 08:36 AM
I am Using LV 2015 and Flatten to XML is not working fine for me, Explained the scenarios in below
Actual json String
{
"root": {
"person": [
{
"name": "Alan",
"url": "http://www.google.com",
"_id": "1"
},
{
"name": "Louis",
"url": "http://www.yahoo.com",
"_id": "2"
}
]
}
}
Expected XML String
<?xml version='1.0' standalone='no'?>
<root>
<person id='1'>
<name>Alan</name>
<url>http://www.google.com</url>
</person>
<person id='2'>
<name>Louis</name>
<url>http://www.yahoo.com</url>
</person>
</root>
Converted XML String using Flatten to string
<String>
<Name></Name>
<Val>{
"root": {
"person": [
{
"name": "Alan",
"url": "http://www.google.com",
"_id": "1"
},
{
"name": "Louis",
"url": "http://www.yahoo.com",
"_id": "2"
}
]
}
}</Val>
</String>
05-22-2017 08:58 AM - edited 05-22-2017 09:04 AM
Your "expected XML" string does NOT match the JSON string setup.
JSON clearly states a struct "person" with three fields (each a string): name, url and _id.
_id does NOT automatically transfer to an attribute of the person in XML, but should remain a field with a custom value.
You have to implement your own string/data type handling algorithm to shift the data appropriately.
Please note that using standard Flatten To XML, you would get something like this for "Alan" if you create a data structure to Unflatten From JSON to LV with your JSON string:
<Cluster>
<Name>person</Name>
<NumElts>3</NumElts>
<String>
<Name>name</Name>
<Val>Alan</Val>
</String>
<String>
<Name>url</Name>
<Val>http://www.google.com</Val>
</String>
<String>
<Name>_id</Name>
<Val>1</Val>
</String>
</Cluster>
EDIT: The XML string i state is like this because of the LabVIEW XML schema:
LabVIEW converts data to an established XML schema. Currently, you cannot create customized schemas, and you cannot control how LabVIEW tags each piece of data. Also, you cannot convert entire VIs or functions to XML.
The predefined XML schema that LabVIEW uses is LVXMLSchema.xsd located in the labview\vi.lib\Utility directory. You can open the file in a text editor to read the schema.
(From the LV Help)
05-23-2017 12:24 AM
Hi Nobert Thanks for the info
I am convinced with the conversion which you shared but the actual the output something which is same as Json.
05-23-2017 02:32 AM
Well, you simply took the JSON string and converted it to LV XML. So what you posted as "that is what i get" is absolutely expected and it MUST look like this.
There is no prewritten function which does the conversion in the way you like it to have. Besides, the DLL you stated must inlcude code to do exactly that: parse the JSON string and put it together in some modified XML string. What you can do is the following:
1. Wrap the DLL if it works the way you want the conversion to be. Build a small API in LV for it.
2. Rewrite the algorithm contained in the DLL (requires reverse engineering?) completely in LV.