Here's a weird one. I'm given an ill-conceived input string that is a list of JSON blobs, separated commas. e.g.:
string input = "{<some JSON object>},{JSON_2},{JSON_3},...,{JSON_n}"
And I have to convert this to an actual list of JSON strings (List<string>
).
For context, the unsanitary "input" list of JSONs is read in directly from a .txt file on disk, produced by some other software. I'm writing an "adapter" to allow this data to be consumed by another piece of software that knows how to interpret the individual JSON objects contained within the list. Ideally, the original software could have output one file per JSON object.
The "obvious" solution (using String.Split
):
List<string> split = input.Split(',').ToList();
would of course fail to escape commas present within the JSON objects ({}
) themselves
I was considering a manual approach - walking the string character-by-character and only splitting out a new element if the count of {
is equal to the count of }
. Something like:
List<string> JsonBlobs = new List<string>();
int start = 0, nestingLevel = 0;
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '{') nestingLevel++;
else if (input[i] == '}') nestingLevel--;
else if (input[i] == ',' && nestingLevel == 0)
{
JsonBlobs.Add(input.Substring(start, i - start));
start = i + 1;
}
}
(The above likely contains bugs)
I had also considered adding JSON array braces on either end of the string ([]
) and letting a JSON serializer deserialize it as a JSON array, then re-serialize each of the array elements one at a time:
List<string> JsonBlobs = Newtonsoft.Json.Linq.JArray.Parse("[" + input + "]")
.Select<Newtonsoft.Json.Linq.JToken, string>(token => token.ToString()).ToList();
But this seems overly-expensive, and could potentially result in newly serialized JSON representations that are not exactly equal to the original string contents.
Any better suggestions?
I'd prefer to use some easily-understandable use of built-in libraries and/or LINQ if possible. Regex would be a last resort, although nifty regex solutions would also be interesting to see.