Vetriramasamy
Vetriramasamy

Reputation: 21

Remove property name from Json when its array

I am converting XML to JSON.

Input:

<emp
    id="17377"/>
<CustomerList>
    <Customer
     id="67149"/>
    <Customer id="64260"/>
   </CustomerList>

OutPut:

"emp": {
      "id": "17377"
    },
"CustomerList": {
      "Customer": [
        {
          "id": "67149"
        },
        {
          "id": "64260"
        }
      ]
    }

But I need the below output. But I can not remove <Customer from <CustomerList> in the input. Also Please note that I need accept dynamic name of array input. But always i want to remove the inner property name to be removed. in this example its Customer.But I may get MarkList->Mark then I need to remove remove Mark, etc.,:

"emp": {
      "id": "17377"
    },
"CustomerList":  [
        {
          "id": "67149"
        },
        {
          "id": "64260"
        }
      ]

Is this possible please.

I use below code to convert XML to Json:

var xml = new XmlDocument();
xml.XmlResolver = null;
xml.LoadXml(richTextBox1.Text);
var jsonText = JsonConvert.SerializeXmlNode(xml,Newtonsoft.Json.Formatting.Indented);

Note:

One solution would be find the char "[" and remove before "[" and after "{".

Upvotes: 1

Views: 732

Answers (1)

Barr J
Barr J

Reputation: 10919

This is not possible, as it is simply trying to change to JSON scheme in which it was orignally built.

what you can do, is use JObject to change the value of customer to feet your needs:

JObject rss = JObject.Parse(json);
JObject customers = rss.SelectToken("CustomerList");
customers ["Customer"] = newValue;

This is the snippet, modify this in your code to feet your needs.

Upvotes: 1

Related Questions