using Newtonsoft.Json.Linq;
public static void Main()
Console.WriteLine("## Test1\n");
Console.WriteLine(ModifyJSON(googleSearchText, "$..", "url", "~~ url masked ~~", Formatting.Indented));
Console.WriteLine("\n\n## Test2\n");
Console.WriteLine(ModifyJSON(googleSearchText, "$.responseData.cursor.pages[?(@.label>=3)]", "label", "-1", Formatting.Indented));
Console.WriteLine("\n\n## Test3\n");
Console.WriteLine(ModifyJSON(googleSearchText, "$.responseData.results[1]", "pages", "null", Formatting.Indented));
Console.WriteLine("\n\n## Test4\n");
Console.WriteLine(ModifyJSON(googleSearchText, "$.responseData.cursor", "pages", "null", Formatting.Indented));
public static string ModifyJSON(string jsource, string jpath, string jnode, string jvalue, Formatting jf = Formatting.None)
JObject jobj = JObject.Parse(jsource);
if (jpath.EndsWith("..")) jpath = jpath + jnode;
else jpath = jpath + "." + jnode;
foreach (var token in jobj.SelectTokens(jpath))
var prop = token.Parent as JProperty;
if (int.TryParse(jvalue, out num)) prop.Value = num;
if (jvalue.Equals("true", StringComparison.OrdinalIgnoreCase)) prop.Value = true;
if (jvalue.Equals("false", StringComparison.OrdinalIgnoreCase)) prop.Value = false;
if (jvalue.Equals("null", StringComparison.OrdinalIgnoreCase)) prop.Value = null;
var array = token.Parent as JArray;
if (jvalue.Equals("null", StringComparison.OrdinalIgnoreCase))
var obj = token.Parent as JObject;
if (jvalue.Equals("null", StringComparison.OrdinalIgnoreCase))
return JsonConvert.SerializeObject(jobj, jf);
static string googleSearchText = @"{
'GsearchResultClass': 'GwebSearch',
'unescapedUrl': 'http://en.wikipedia.org/wiki/Paris_Hilton',
'url': 'http://en.wikipedia.org/wiki/Paris_Hilton',
'visibleUrl': 'en.wikipedia.org',
'cacheUrl': 'http://www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org',
'title': '<b>Paris Hilton</b> - Wikipedia, the free encyclopedia',
'titleNoFormatting': 'Paris Hilton - Wikipedia, the free encyclopedia',
'content': '[1] In 2006, she released her debut album...'
'GsearchResultClass': 'GwebSearch',
'unescapedUrl': 'http://www.imdb.com/name/nm0385296/',
'url': 'http://www.imdb.com/name/nm0385296/',
'visibleUrl': 'www.imdb.com',
'cacheUrl': 'http://www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com',
'title': '<b>Paris Hilton</b>',
'titleNoFormatting': 'Paris Hilton',
'content': 'Self: Zoolander. Socialite <b>Paris Hilton</b>...'
'estimatedResultCount': '59600000',
'moreResultsUrl': 'http://www.google.com/search?oe=utf8&ie=utf8...'