0

I've some JSON data coming back as a string that looks like (copy and paste from VS text visualizer):

{
   "error":0,
   "result":{
      "name":"wjetestuser1",
      "id":"0eu0_User_2_0b4cfb616e648d4792056c1a6e7d801e_null",
      "status":"ACTIVE"
   }
}
{
   "match":[
      [
         "domain.id",
         "=",
         "2"
      ],
      [
         "loginName",
         "=",
         "wjetestuser1"
      ]
   ],
   "return":[
      "name",
      "id",
      "status"
   ]
}

I'm trying to turn this into a List for everything after the "result": and before {"match": without using a replace command, so I'll end up with a list that looks something like:

Name, wjetestuser1
id, 0eu0_User_2_0b4cfb616e648d4792056c1a6e7d801e_null
status, ACTIVE

If I can get the error code status back thats a bonus, but really not needed.

I'm hoping there is a simple one (or a few liners) that don't involve hacking the string apart with a replace regex command.

Various code attempts so far, but this worked for me if I strip before and including "result": and after and including {"match":

s below is the output above as a single line

s = commonCode.ExeApiCall(url);
var DSData = new List<KeyValuePair<string, string>>();
var jsonData = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(s);

errors at the 3rd line down with error:

Additional text encountered after finished reading JSON content: {. Path '', line 1, position 119.

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Wayne Evans
  • 157
  • 13

1 Answers1

1

As Sir Rufo pointed out in this comment if you use Newtownsoft library you can Read Multiple Fragments With JsonReader. Example from the website:

public class Role
{
    public string Name { get; set; }
}

public class Main
{
    string json = @"{ 'name': 'Admin' }{ 'name': 'Publisher' }";
    IList<Role> roles = new List<Role>();

    JsonTextReader reader = new JsonTextReader(new StringReader(json));
    reader.SupportMultipleContent = true;

    while (true)
    {
        if (!reader.Read())
            break;

        JsonSerializer serializer = new JsonSerializer();
        Role role = serializer.Deserialize<Role>(reader);
        roles.Add(role);
    }

    foreach (Role role in roles)
        Console.WriteLine(role.Name);
}

My original answer was making minimal JSON parser to this particular case scenario. See revision history for that.

  • Thank you. the top set of code worked straight off. Second set, I put my string 's' between the brackets of .ToJsons(s) and it came out perfect. This was a lot tidier than the regex mess I was working with. – Wayne Evans Aug 03 '17 at 16:55
  • 1
    @WayneEvans fyi friend: it's impossible to solve it with regex. you have to use stack. [some reference to study](https://en.wikipedia.org/wiki/Chomsky_hierarchy) – Vanity Slug - codidact.com Aug 03 '17 at 17:12
  • 1
    Try your extension method with this **valid** JSON string *{"foo}{5":"bar}{6"}* – Sir Rufo Aug 03 '17 at 21:34
  • @alex You have to write a (nearly) complete json parser to split the root elements, which can be an object or an array as well. If you are going to write one, I can give you some JSON examples for the unit tests :o) – Sir Rufo Aug 03 '17 at 21:53
  • 1
    @alex No need to implement a feature already implemented by Json.NET (see my last comment at the question) – Sir Rufo Aug 06 '17 at 07:53