How extract some value from JArray and put into a JSon

I have the next JObject:

{
  "Attrib1":"Value1",
  "Atrrib2":"Value2",
  "Inners": [
    {
      "InnerAttrib1":"InnerValue1",
      "InnerAttrib2":"InnerValue2"
    },
    {
      "InnerAttrib1":"InnerValue2",
      "InnerAttrib2":"InnerValue3"
    },
    {
      "InnerAttrib1":"InnerValue4",
      "InnerAttrib2":"InnerValue5"
    }
  ],
  "Attrib3": "anotherThing"
}

I have a function that extract the "Inner" values and make a REST call, my REST service only supports one "Inners" attrib at the time,just like that for example:

   {
      "Attrib1":"Value1",
      "Atrrib2":"Value2",
      "Inners": [
        {
          "InnerAttrib1":"InnerValue2",
          "InnerAttrib2":"InnerValue3"
        }
      ],
      "Attrib3": "anotherThing"
    }

so...I've made a function that extract one value from "Inners":

public List<string> fillClient(JObject request)
        {
            var auxiliarJson = new JObject();
            var arraytoMerge=new JArray();
            var listResult = new List<string>();
            clientRest.EndPoint = "ClientEndPoint";
            auxiliarJson = request;
            arraytoMerge = request.SelectToken("Inners") as JArray;            
            for (int innerCount = 0; innerCount < arraytoMerge.Count; innerCount++)
            {
              auxiliarJson.SelectToken("Inners").Replace(arraytoMerge[innerCount]);//My problem!
              clientRest.PostData = request.ToString(); //fill the data to send
              listResult.Add(clientRest.MakeRequest()); //Send the request 
            }
            return listResult;
        }

When I make this I have an error (Bad Request), debugging I found that my "Inners" is just like that:

"Inners": "{
          "InnerAttrib1":"InnerValue1",
          "InnerAttrib2":"InnerValue2"
        }"

How can eliminate this quotes in the start and finish of the brackets, or if you can suggest to me some alternative to make the same, thanks!

Upvotes: 1

Views: 761

Answers (2)

Trying with some things I found the way to do:

auxiliarJson.SelectToken("Inners").Replace(JToken.Parse("["+arraytoMerge[innerCount].ToString()+"]")); //My Solution!

Upvotes: 1

Falanwe
Falanwe

Reputation: 4744

My advice would be do not work with JObject unless you have no other choice.

If you know the "schema" of your json (and in most cases you know exactly what to expect and just ignore the unexpected), then you can deserialize your json in a POCO. You can then manipulate your strongly typed data at will, then serialize it back when you need to send it.

public class MyClass
{
    public string Attrib1 {get; set;}
    public string Attrib2 {get; set;}
    public List<InnerClass> Inners {get; set;}
    ...
}

public class InnerClass
{
    ...
}

public void DoStuffWithInput(string input)
{
    var myObject = JsonConvert.DeserializeObject<MyClass>(input);
    foreach (InnerClass inner in myObect.Inners)
    {
        var innerJson = JsonConvert.SerializeObject(inner);
        // do stuff
    }
}

Upvotes: 0

Related Questions