1
votes

I'm trying to parse a reponse I get back from an API that returns data in the odata format.

The data contains all the data I need in an array(value), but I can't figure out how to get at that data and remove the root/parent element that holds it.

I'm returning this data to a javascript MVC frontend, so I need it to be in JSON format, but without it's parent.

The code I have just returns the whole odata object.

I don't need the parent part that says, ""odata.metadata":"http://systech.edu/SOM_a/Api/v1/$metadata#Keys":

Here is the odata returned by the API:

{
  "odata.metadata":"http://systech.edu/SOM_a/Api/v1/$metadata#Keys","value":[
    {
      "odata.id":"http://systech.edu/SOM_a/Api/v1/Keys('111')","Id":"111","Name":"VideoSearch","Timeout":20160,"IsDefault":false,"Auth":false,"err":false,"exclude":true,"Internal":true,"view":true
    },{
      "odata.id":"http://systech.edu/SOM_a/Api/v1/Keys('222')","Id":"222","Name":"DesktopRecorder","Timeout":20160,"IsDefault":false,"Auth":false,"err":false,"exclude":true,"Internal":true,"view":false
    },{
      "odata.id":"http://systech.edu/SOM_a/Api/v1/Keys('333')","Id":"333","Name":"Manage","Timeout":20160,"IsDefault":false,"Auth":false,"err":false,"exclude":true,"Internal":true,"view":false
    }
  ]
}

Here is the code that handles the api and odata:

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;


            WebRequest request = WebRequest.Create("http://systech.edu/som_a/api/V1/Keys");
            request.ContentType = "application/json; charset=utf-8";
            SetBasicAuthHeader(request, "xxxx", "xxxx");
            WebResponse response = request.GetResponse();
            var rawJson = new StreamReader(response.GetResponseStream()).ReadToEnd();

            json = JObject.Parse(rawJson);

desired JSON output:

{
  "odata.id":"http://systech.edu/SOM_a/Api/v1/Keys('111')","Id":"111","Name":"VideoSearch","Timeout":20160,"IsDefault":false,"Auth":false,"err":false,"exclude":true,"Internal":true,"view":true
},{
  "odata.id":"http://systech.edu/SOM_a/Api/v1/Keys('222')","Id":"222","Name":"DesktopRecorder","Timeout":20160,"IsDefault":false,"Auth":false,"err":false,"exclude":true,"Internal":true,"view":false
},{
  "odata.id":"http://systech.edu/SOM_a/Api/v1/Keys('333')","Id":"333","Name":"Manage","Timeout":20160,"IsDefault":false,"Auth":false,"err":false,"exclude":true,"Internal":true,"view":false
}
1
try this var ODataJSON = JsonConvert.DeserializeObject<JObject>(json); ODataJSON.Property("odata.metadata").Remove(); - warrior

1 Answers

1
votes

This should work.

var ODataJSON = JsonConvert.DeserializeObject<JObject>(json); ODataJSON.Property("odata.metadata").Remove();