skip to Main Content

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?

2

Answers


  1. You should take a look at this bit of doc

    string json = @"...";
    XmlDocument doc = (XmlDocument) JsonConvert.DeserializeXmlNode(json);
    

    Now from what I see, vestigingsAdres and correspondentieAdres look as if they are the same type of class object, but given how NewtonSoft works, most likely they will not be grouped under a collection. I can foresee that you may want this to potentially be a Dictionary<string, yourObjHere> so if that is the case, you will need to intervene by constructing the objects in memory and then serializing to JSON. To do this, you can use JsonConvert.DeserializeObject<yourObj>(jsonString); if you have already modeled the class object

    If you are fine with just straight up dumping to XML as is, then don’t worry

    Login or Signup to reply.
  2. 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",
       .....
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search