1

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 "{".

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Barr. But the array name will be dynamic input. So I can not hard code like "CutomerList". The only hind is its array.
then parse the object and give it the dynamic value of the inner array, this can be as dynamic as it gets :)
@ Barr, CutomerList is just part of the whole json.
The example still stands :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.