Doing a simple JObject.parse(inJson) from Newtonsoft.json gives me the error Unable to cast object of type 'Newtonsoft.Json.Linq.JValue' to type 'Newtonsoft.Json.Linq.JObject

Awesome!

That would be useful to know earlier :slight_smile:

Try this:

        private string RestrictStringTokensToLength(string inJson, int maxLength)
        {
            JObject obj = JObject.Parse(inJson);

            IEnumerable<JValue> stringTokens = obj.Descendants()
                                  .OfType<JValue>()
                                  .Where(v => v.Type == JTokenType.String);

            foreach (JValue token in stringTokens)
            {
                string tokenValue = token.Value.ToString();
                if (tokenValue.Length > maxLength)
                {
                    token.Value = tokenValue.Substring(0, maxLength);
                }
            }
            return JsonConvert.SerializeObject(obj, Formatting.None);
        }