Home > other >  remove from a json object
remove from a json object

Time:08-09

I have a JSON object. I need to remove dynamicObject from the JSON, but not the fields under it. example this is how I am passing it

var item = JsonConvert.SerializeObject(someRequest); Below is the result.

{
  "email_address": "[email protected]",
  "status": "subscribed",
  "merge_fields": {
    "dynamicObject": {
      "FirstName": "Jack",
      "LastName": "Sparrow"
    }
  }
}

it should look like

{
  "email_address": "[email protected]",
  "status": "subscribed",
  "merge_fields": {
    "FirstName": "Jack",
    "LastName": "Sparrow"
  }
}

CodePudding user response:

I would recommend to create a temporary anonymous object that has the structure that you need, and then Serialize that:

var obj = new
{
    email_address = someRequest.email_address,
    status = someRequest.status, 
    merge_fields = someRequest.merge_fields.dynamicObject
};
var item = JsonConvert.SerializeObject(obj);

CodePudding user response:

try this

var jsonParsed=JObject.Parse(item);

jsonParsed["merge_fields"]=jsonParsed["merge_fields"]["dynamicObject"];

item=jsonParsed.ToString();
  • Related