Check whether JSON property contains value or array

1.4k Views Asked by At

I have a random input XML file which I am trying to convert into JSON and access values. Now JSON might have a simple JSON object or it might have array within it. When I am working with simple JSON object I am able to get values properly but when any JSON property contains an array I am getting an error that "value cannot be null". I am using newtonsoft library for this. My question is how to know whether a property is having a value or an array in JSON when every time the input file might have different structure. I hope I am able to explain my problem and the code I am using is given below:

            XmlDocument Xmldoc = new XmlDocument();
            Xmldoc.LoadXml(fileContents);
            string root = Xmldoc.DocumentElement.Name;
            var json = JsonConvert.SerializeXmlNode(Xmldoc);
            var JsonStringObject = (JObject)JsonConvert.DeserializeObject(json);
            var XMLNodesData = JsonStringObject[root.ToString()].Children();
            List<JToken> tokens = XMLNodesData.Children().Children().ToList();
            
            foreach (var node in tokens)
            {
                foreach (Field field in fields)
                {
                    
                    var inputString = node[field.FieldName].Value<string>();
                }
            }

With above code I am able to get value properly on inputString if I am having a simple XML such as :

<PatientRecords>
    <Patient>       
        <firstname>John</firstname>
        <lastname>Smith</lastname>      
        <patientid>111</patientid>
        <dob>2022-05-13</dob>
    </Patient>
    <Patient>
        <firstname>Martha</firstname>
        <lastname>Stewart</lastname>        
        <patientid>112</patientid>
        <dob>2022-04-14</dob>
    </Patient>
</PatientRecords>

but if I Change XML to below given format then I am getting an error "Value cannot be null" incase of age property:

<PatientRecords>
    <Patient>       
        <firstname>John</firstname>
        <lastname>Smith</lastname>      
        <patientid>111</patientid>
        <age>
            <dob>2022-05-13</dob>
        </age>
    </Patient>
    <Patient>
        <firstname>Martha</firstname>
        <lastname>Stewart</lastname>        
        <patientid>112</patientid>
        <age>
            <dob>2022-04-14</dob>
        </age>
    </Patient>
</PatientRecords>

Note: field.Fieldname is used to get tag name ("firstname", "lastname" etc) from a different XML which is required in my project. Please help regarding this and thank you in advance.

1

There are 1 best solutions below

0
On

You can try below

var jsonResult = JsonConvert.DeserializeObject<dynamic>(jsonStr);
            
            if(jsonResult.Type==JTokenType.Object)
            {
                //JSON contains Object
            }
            else if(jsonResult.Type==JTokenType.Array)
            {
                //JSON contains Array 
            }