0

I need to be able to enumerate through ANY kind of JSON, multi-dimensional, one-dimensional, nested, e.t.c and be able to search through BOTH Key, Values e.t.c for <TECH> (.Contains not ==) and replace it with I'm Now Here! "\(^.^)/"

Example: {"hi":"hello","hi<TECH>":'Lol<TECH>,,,'}

Result should be: {"hi":"hello","hiI'm Now Here! \"\\(^.^)\/\"":'LolI\'m Now Here! "\\(^.^)\/",,,'}

Im essentially trying to be able to insert data where <TECH> is while properly taking care of \" \ and \' (and others like this).

Again it may not be a single dimensional line it could be nested. Im trying to JObject.Parse() which allows me to Enumerate through KeyValuePairs but how im meant to send back an edit to the Key/Value im unsure of.

(Also ordering and such is important, I need it to end up being completely the same as the original JSON, formatting and even how its formatted as in one-lined or not has to stay the same)

What I have at the moment

JObject j = JObject.Parse(postData);
foreach (KeyValuePair<string, JToken> item in new JObject(j)) {
    string OriginalKey = item.Key.ToString();
    string OriginalValue = item.Value.ToString();
    string techWord1 = "Other Word's Stuff \":P";
    string techWord2 = "I'm Now Here! \"\\(^.^)/\"";
    foreach (string techWord in new string[] { "TECH1", "TECH2" }) {
        if (OriginalKey.Contains("<" + techWord + ">")) {
            j[OriginalKey.Replace("<" + techWord + ">", techWord == "TECH1" ? techWord1 : techWord2)] = j[item.Key];
            j.Remove(item.Key);
        }
        if(OriginalValue.Contains("<" + techWord + ">")) {
            j[item.Key] = OriginalValue.Replace("<" + techWord + ">", techWord == "TECH1" ? techWord1 : techWord2);
        }
    }
}
return j.ToString(Formatting.None);

Issues:

  • Does not follow the same exact spacing, indenting e.t.c as the original
  • Don't think it works on nested/multi-dimensional JSON
  • Don't think every kind of JObject is a KeyValuePair Format? For example how is it going to handle: [{"hi":"value"}, {"hi2":"value2"}]

Other than those, this seems like a good option. It properly parses all kinds of EOF errors that could occur in both Key:Value scenarios. Just need some way to make its .ToString format EXACTLY like the input JSON, for example lets say there were 2 spaces between { and "hi" in the input JSON, the .ToString() would remove that but I need it to KEEP those.

Ma Dude
  • 477
  • 1
  • 5
  • 17
  • *Im trying to JObject.Parse()* -- then can you share what you have tried that does not work -- i.e. a [mcve]? Your question is unclear as to what exactly you are doing. Maybe you want [JSON.NET - Select All Objects](https://stackoverflow.com/q/46611888)? Maybe [How to do recursive descent of json using json.net?](https://stackoverflow.com/q/16181298)? Or maybe [With json.net, is there a shortish way to manipulate all string fields?](https://stackoverflow.com/q/46148971/3744182) and [Json.net rename properties](https://stackoverflow.com/q/11679804)? – dbc Oct 15 '18 at 17:17
  • @dbc Gotchu fam edited – Ma Dude Oct 15 '18 at 17:32
  • Why not try `JsonConvert.DeserializeObject`? – Frontear Oct 15 '18 at 17:49
  • @Frontear What would I deserialize to that could help me better than my situation above? Remember I need to convert it back aswell. – Ma Dude Oct 15 '18 at 17:58
  • If you need to preserve indentation **exactly** then Json.NET may not work for you. An enhancement request [Add an option to preserve whitespace in JTokens #180](https://github.com/JamesNK/Newtonsoft.Json/issues/180) was declined. But are you really sure you need that? The [JSON standard](http://json.org/) says *Whitespace can be inserted between any pair of tokens.* which means that it's inconsequential. – dbc Oct 15 '18 at 19:09
  • @MaDude Write a recursive analyzer for a JToken that scans it's input for the key, substitutes it if necessary and then calls itself again if there are nested elements found. That's how you could resolve your issue with not being able to work with nested/multi-dimensional JSON strings. – Manuel Fuchs Oct 15 '18 at 21:42

0 Answers0