-2

I have an azure function and which receives the request as below . One of the property has xml value . But when I desearialize it to json ,i'm getting the error.

{
    "policyVersionedApiName": "petstoreoperationpolicyapi",
    "operationPolicies": [
        {
            "policyFormat": "rawxml",
            "operationId": "createUsersWithArrayInput",
             "policyValue": "<policies> <inbound> <base/> <ip-filter action="allow"> <address>10.100.7.1</address> </ip-filter> </inbound> </policies>",
        }
       
    ]
}

1 Answers1

0

you have to replace double quotes inside of json string by escape double quoutes

json = json.Replace("\">", "\\\">").Replace("=\"", "=\\\"");

If you need , you can convert xml to json too

    var jsonParsed = JObject.Parse(json);

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml((string)jsonParsed["operationPolicies"][0]["policyValue"]);
    XmlNode node = xmlDoc.SelectSingleNode("policies");

    jsonParsed["operationPolicies"][0]["policyValue"] = JObject.Parse(JsonConvert.SerializeXmlNode(node));
    
    json = jsonParsed.ToString();

a new json

{
  "policyVersionedApiName": "petstoreoperationpolicyapi",
  "operationPolicies": [
    {
      "policyFormat": "rawxml",
      "operationId": "createUsersWithArrayInput",
      "policyValue": {
        "policies": {
          "inbound": {
            "base": null,
            "ip-filter": {
              "@action": "allow",
              "address": "10.100.7.1"
            }
          }
        }
      }
    }
  ]
}
Serge
  • 40,935
  • 4
  • 18
  • 45