LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Conversion from XML to Json and Vive Versa

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.

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 1 of 7
(5,336 Views)

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).

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 2 of 7
(5,328 Views)

Currently using LV2015

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 3 of 7
(5,306 Views)

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>

 

 

 

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 4 of 7
(5,293 Views)

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)

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
Message 5 of 7
(5,286 Views)

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.

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 6 of 7
(5,261 Views)

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.

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 7 of 7
(5,252 Views)