Search code examples
c#jsonxmljson.net

How to convert a JSON string array to XML correctly


I have a question about correctly representing a JSON string array in XML. I want to build the following JSON data (expected API input JSON):

{
  "relatiesoort": [
    "string"
  ],
  "modifiedOn": "string",
  "relatiecode": 0,
  "naam": "string",
  "vestigingsAdres": {
    "contactpersoon": "string",
    "straat": "string",
    "postcode": "string",
    "plaats": "string",
    "land": {
      "id": "00000000-0000-0000-0000-000000000000",
      "uri": "string"
    }
  },
  "correspondentieAdres": {
    "contactpersoon": "string",
    "straat": "string",
    "postcode": "string",
    "plaats": "string",
    "land": {
      "id": "00000000-0000-0000-0000-000000000000",
      "uri": "string"
    }
  },
  "telefoon": "string",
  "mobieleTelefoon": "string"
}

I initially want to build this in XML format and then convert it to JSON. I initially build the following XML:

<?xml version="1.0" encoding="UTF-8" ?> 
        <root>   
    <relatiesoort>string</relatiesoort>   
    <modifiedOn>string</modifiedOn>   
    <relatiecode>0</relatiecode>   
    <naam>string</naam>   
    <vestigingsAdres>
                <contactpersoon>string</contactpersoon>
                <straat>string</straat>
                <postcode>string</postcode>
                <plaats>string</plaats>
                <land>
                  <id>00000000-0000-0000-0000-000000000000</id>
                  <uri>string</uri>
                </land>   
    </vestigingsAdres>   
    <correspondentieAdres>
                <contactpersoon>string</contactpersoon>
                <straat>string</straat>
                <postcode>string</postcode>
                <plaats>string</plaats>
                <land>
                  <id>00000000-0000-0000-0000-000000000000</id>
                  <uri>string</uri>
                </land>   
    </correspondentieAdres>   
    <telefoon>string</telefoon>   
    <mobieleTelefoon>string</mobieleTelefoon> 
    </root>

..... and then I use NewtonSoft.Json to translate it back to Json and feed it to the API.

But the problem is that if I convert it back to Json (this can also be done with this website: https://www.freeformatter.com/xml-to-json-converter.html) I get this result:

{
  "relatiesoort": "string",
  "modifiedOn": "string",
  "relatiecode": "0",
  "naam": "string",
  "vestigingsAdres": {
    "contactpersoon": "string",
    "straat": "string",
    "postcode": "string",
    "plaats": "string",
    "land": {
      "id": "00000000-0000-0000-0000-000000000000",
      "uri": "string"
    }
  },
  "correspondentieAdres": {
    "contactpersoon": "string",
    "straat": "string",
    "postcode": "string",
    "plaats": "string",
    "land": {
      "id": "00000000-0000-0000-0000-000000000000",
      "uri": "string"
    }
  },
  "telefoon": "string",
  "mobieleTelefoon": "string"
}

As you can see the 'relatiesoort field' is not a JSON array anymore and the API gives an error as the JSON is not in the expected format. What I want is to build the XML in a way that the relatiesoort field remains a Json array after converting it back to JSON.

To accomplish that: should I format this field as followed in XML?:

<relatiesoort>
    <element>klant</element>
</relatiesoort>

How can I accomplish this with the NewtonSoft.Json library?


Solution

  • Nothing points that relatiesoort is an array, You can't automatically convert string to array of strings, to convert automatically it shoud be

    <relatiesoort>string</relatiesoort> 
    <relatiesoort>string</relatiesoort> 
    

    in this case you need c# class with an array xml attributes of relatiesoort property. So the only way to make it correct, you need to convert XML To c# and serialize to a json string. If you don't have the class, another way is to parse your json and correct the value

    var jObj = JObject.Parse(json);
    
    if (jObj["relatiesoort"].Type != JTokenType.Array) 
    {
    jObj["relatiesoort"] = new JArray {jObj["relatiesoort"]};
    json = jObj.ToString();
    }
    

    json

    {
      "relatiesoort": [
        "string"
      ],
      "modifiedOn": "string",
       .....
    }