VICTOR GALVAO · Oct 23, 2020

How to convert XML to JSON from a %XML.TextReader? (Without a custom method)

Hi guys!!

Anyone of you, someday needed to convert a parsed XML (With %XML.TextReader), to a DynamicObject on caché? If anyone can help me, I'm grateful.

0 0 2 253
Log in or sign up to continue


Hi Victor,

I assume that you need Dynamic Objects in order to create some JSON Output (so you can call %ToJSON() on the object to create a JSON string). So in my opinion, the complexity of your JSON object will drive these decisions - for example, how deeply nested will your JSON need to be?

From some familiarity with TextReader, it records every XML Element into a Property. So in theory, each Property can have its corresponding Dynamic Object which will be a Name/Value pair: Name = Name of Property; Value = Value of Property (i,e. XML Text Node)

It is hard to say more without seeing your XML structure or your JSON Spec. You may find the following links helpful:

Here's how you can have a bidirectional XML<>JSON converter:

  1. If you do not have the class for your XML, create it from XSD or manually. Also  enable JSON for it.
  2. Convert your XML string into an object of class (1).
  3. Export JSON from your object.