-1

My source data is in the form of

[["value11", "value12"],["value13", "value14"]]
[["value21", "value22"]]
[["value31", "value32"],["value33", "value34"], ["value35", "value36"]]

and not

[["Key1" : "value11", "Key2" : "value12"],["Key1" : "value13", "Key2" : "value14"]]
[["Key1" : "value21", "Key2" : "value22"]]
[["Key1" : "value31", "Key2" : "value32"],["Key1" : "value33", "Key2" : "value34"], ["Key1" : "value35", "Key2" : "value36"]]

and this is a large file. JSON.Net is not able to parse this. Is there any parser already available? From here, I can use Regex to replace and format the data but I would rather not meddle the data.

Pankaj Parag
  • 437
  • 1
  • 6
  • 17
  • 4
    That's not json... – Marcel Oct 25 '18 at 21:34
  • That's not valid json (no separators between lines, etc), so it makes sense that JSON.Net can't parse it. Do the line breaks have any significance? I don't recognize the format, so not sure if there are serializers for it or not; failing that, string manipulation (split on line return for records, strip leading and trailing brackets from each line, and split on comma) might work? – Jonathan Oct 25 '18 at 21:37
  • @Jonathan: split on line return for records, strip leading and trailing brackets from each line, and split on comma. There is one comma inside the bracket, one outside the bracket. If I can get ["value1" , "value2"] in a single string, I can work with that. – Pankaj Parag Oct 25 '18 at 21:45

2 Answers2

1

That is json!

You can deserialize every line to a List<string[]>:

string s = "[[\"value11\", \"value12\"],[\"value13\", \"value14\"]]";
var a = JsonConvert.DeserializeObject<List<string[]>>(s);
skyoxZ
  • 362
  • 1
  • 3
  • 10
0

You may be confused with JSON arrays and objects:

  • Objects = an unordered set of name/value pairs, e.g. {"name":"value"}
  • Arrays = an ordered collection of values, e.g. [1, 2, {"pi":3.14159265359}]

Your second example is not correct because you are putting name/value pairs in an array instead of an object:

[{"Key1" : "value11", "Key2" : "value12"},{"Key1" : "value13", "Key2" : "value14"}]
[{"Key1" : "value21", "Key2" : "value22"}]
[{"Key1" : "value31", "Key2" : "value32"},{"Key1" : "value33", "Key2" : "value34"}, {"Key1" : "value35", "Key2" : "value36"}]

However, each line by itself is valid JSON, but not the entire contents. You either need to:

  1. Process each line individually
  2. Put the entire contents into an array with comma separators (change the JSON generation)

For example, if you wanted the entire contents to be valid, then your first test case would be:

[
  [["value11", "value12"], ["value13", "value14"]],
  [["value21", "value22"]],
  [["value31", "value32"],["value33", "value34"], ["value35", "value36"]]
]

Note that this has an outer array and each element is an array of array (3 levels of arrays!).

It isn't clear how you intend to use the data, so that is all I can tell you for now.

Ryan
  • 7,835
  • 2
  • 29
  • 36