0

How to keep original casing? (JsonWriter WriteTo always converts to camel casing)

Sample JSON

{
   "order":"123",
   "item_xyz":{
      "some_property":"some value",
      "another_property":null,
      "yet_another_property":123
   }
}

My method

    public string JSONSelect(string Json, string JsonPath)
    {

        string jsonResult = string.Empty;

        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);

        using (JsonWriter json = new JsonTextWriter(sw))
        {
            json.Formatting = Newtonsoft.Json.Formatting.None;

            JToken res = JToken.Parse(Json).SelectToken(JsonPath); //http://www.newtonsoft.com/json/help/html/SelectToken.htm
            if (res != null)
            {
                switch (res.Type)
                {
                    case JTokenType.String:
                        sb.Append(((JValue)res).ToString());
                        break;
                    case JTokenType.Null:
                        break;
                    default:
                        res.WriteTo(json); //<-----------here!
                        break;
                }
            }
        }
        jsonResult = sb.ToString();
        return jsonResult;
    }
001
  • 62,807
  • 94
  • 230
  • 350
  • Hope this link http://stackoverflow.com/questions/34070459/newtonsoft-jsonserializer-lower-case-properties-and-dictionary And http://www.newtonsoft.com/json/help/html/ModifyJson.htm can help you – Chandan Kumar Mar 24 '17 at 09:33
  • look into https://github.com/aspnet/Mvc/commit/d8d2e54506c7b1973156316f1dd82c6226de2c95#comments too – Chandan Kumar Mar 24 '17 at 09:37
  • 1
    You need to provide more information on how to reproduce the problem. [It works fine for me](https://dotnetfiddle.net/cHEE0T). – Brian Rogers Mar 24 '17 at 15:08

0 Answers0