Home > other >  Remove Prefix Tags from JSON Passed to a WCF Service in c#
Remove Prefix Tags from JSON Passed to a WCF Service in c#

Time:12-28

We are passing JSON request to a WCF service which comprises some additional prefixes like xy1, xy2 and “@” sign etc which makes it customized JSON. Our WCF service’s create order method process this customized JSON to create orders. We want to pass standard JSON to WCF service instead of customized one. Is there any way to pass standard JSON to WCF service and then it converts back to customized JSON with minimum code changes.

NOTE: This is sample JSON but actual JSON file is long and comprises more details.


customized JSON is as following:

{
    "xy1:createOrder": {
        "@test:xy1": "http://www.testing.com/schema/order",
        "@test:xy2": "http://www.testing.com/schema/customer",
        "xy1:order": {
            "@Date": "2022-12-27",
            "@orderNo": "FirstOrder1"
        },
        "xy2:customer": {
            "@Id": "1",
            "xy2:title": "Mr",
            "xy2:fName": "Alex",
            "xy2:lName": "John",
            "xy2:emailId": "[email protected]"
        }
    }
}


standard JSON is as following:

{
    "createOrder": {
        "order": {
            "Date": "2022-12-27",
            "orderNo": "FirstOrder1"
        },
        "customer": {
            "Id": "1",
            "title": "Mr",
            "fName": "Alex",
            "lName": "John",
            "emailId": "[email protected]"
        }
    }
}

CodePudding user response:

you can try this code

    var origJObj = JObject.Parse(json).Dump();
    var cleanedJObj = new JObject();
    
    CleanJsonObject(origJObj, cleanedJObj);

    //if you need a json
    json = cleanedJObj.ToString();


public void CleanJsonObject(JObject origJObj, JObject cleanedJObj)
{
    foreach (var prop in origJObj.Properties())
    {
        var name = "";
        var startIndex = prop.Name.IndexOf(":");
        if (startIndex > 0)
        {
            name = prop.Name.Substring(startIndex   1);
        }
        else name = prop.Name.Replace("@", "");
        if (prop.Value.Type == JTokenType.Object)
        {
            var newJObj = new JObject();
            CleanJsonObject((JObject)prop.Value, newJObj);
            cleanedJObj.Add(name, newJObj);
        }
        else
        {
            cleanedJObj.Add(name, prop.Value);
        }
    }
}

CodePudding user response:

In addition to eliminating unnecessary parts of your code, try josn.net to customize your requirements.

An example of how to use it:

public class User
{
    public User(string json)
    {
        JObject jObject = JObject.Parse(json);
        JToken jUser = jObject["user"];
        name = (string) jUser["name"];
        teamname = (string) jUser["teamname"];
        email = (string) jUser["email"];
        players = jUser["players"].ToArray();
    }

    public string name { get; set; }
    public string teamname { get; set; }
    public string email { get; set; }
    public Array players { get; set; }
}

// Use
private void Run()
{
    string json = @"{""user"":{""name"":""asdf"",""teamname"":""b"",""email"":""c"",""players"":[""1"",""2""]}}";
    User user = new User(json);

    Console.WriteLine("Name : "   user.name);
    Console.WriteLine("Teamname : "   user.teamname);
    Console.WriteLine("Email : "   user.email);
    Console.WriteLine("Players:");

    foreach (var player in user.players)
        Console.WriteLine(player);
 }
  • Related