Parse download of JSON data from URL

MicroMan Source

I have some JSON data which I have downloaded from a URL as .gz file, I then decompressed, and read the JSON as a string

I am trying to loop through the records

But neither JArray or JObject seem to work

Any ideas how to read in this data, object by object?

{"user_id":"25c6b8080aa26","email":"[email protected]","roles":"Manager"}
{"user_id":"1a25c6b8080b6ac","email":"[email protected]"}
c#arraysjsonjson.net

Answers

answered 3 months ago Nkosi #1

The provided sample is not a valid JSON document but appear to contain snippets of JSON objects.

Assuming each record/object is confined to a single line you can read the file line by line and convert the objects

The provider data would parse to

public class user {
    public string user_id { get; set; }
    public string email { get; set; }
    public string roles { get; set; }
}

And following can be used to deserialise the decompressed file

public class MyParser {
    public IEnumerable<T> Parse<T>(string path) {
        var lines = File.ReadLines(path);
        foreach (var line in lines) {
            var record = JsonConvert.DeserializeObject<T>(line);
            yield return record;
        }
    }
}

Getting the desired objects then would be something like

var path = "path to decompressed file";
var parser = new MyParser(); 
var users = parse.Parse<user>(path);
foreach(var user in users) {
    //...
}

comments powered by Disqus