1

I have read a lot forums and still can't seem to piece this together. I am using Json.Net to parse a JSON file. It is extremely large and I just want one piece of it. I make the call and parse it like this:

string json = string.Empty;
HttpWebResponse response; 
string url = @""; 
HttpWebRequest request = (HttpWebRequest)(WebRequest.Create(url)); 

using (response = (HttpWebResponse)request.GetResponse()) 
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
        json = reader.ReadToEnd();
}

JObject obj = JObject.Parse(json);

Like I said this JSON is huge but it is just a very long list of contacts. Here's what a single contact contains:

{
"contacts": [
    {
        "addedAt": 1234,
        "vid": 1234,
        "canonical-vid": 1234,
        "merged-vids": [
            1234,
            1234
        ],
        "portal-id": 1234,
        "is-contact": true,
        "profile-token": "": "",
        "properties": {
            "country": {
                "value": "united states"
            },
            "firstname": {
                "value": "Example"
            },
            "jobtitle": {
                "value": "Developer"
            },
            "primary_role": {
                "value": "Developer"
            },
            "lastmodifieddate": {
                "value": "1234"
            },
            "state": {
                "value": "CO"
            },
            "email": {
                "value": "[email protected]"
            },
            "lastname": {
                "value": "Example"
            }
        },
        "form-submissions": [
            {
                "conversion-id": "1234",
                "timestamp": 1234,
                "form-id": "",
                "portal-id": 1234,
                "page-url": "",
                "page-title": "",
                "title": "",
                "form-type": "",
                "meta-data": []
            }
        ],
        "identity-profiles": [
            {
                "vid": 1234,
                "saved-at-timestamp": 1234,
                "deleted-changed-timestamp": 234,
                "identities": [
                    {
                        "type": "EMAIL",
                        "value": "[email protected]",
                        "timestamp": 1243,
                        "is-primary": true
                    },
                    {
                        "type": "TYPE",
                        "value": "",
                        "timestamp": 1234
                    },
                    {
                        "type": "EMAIL",
                        "value": "[email protected]",
                        "timestamp": 1234,
                        "is-secondary": true
                    }
                ]
            },
            {
                "vid": 1234,
                "saved-at-timestamp": 1324,
                "deleted-changed-timestamp": 0,
                "identities": [
                    {
                        "type": "TYPE",
                        "value": "",
                        "timestamp": 1234
                    }
                ]
            }
        ],
        "merge-audits": [
            {
                "canonical-vid": 1234,
                "vid-to-merge": 1234,
                "timestamp": 1324,
                "entity-id": "",
                "user-id": 1234,
                "num-properties-moved": 234,
                "merged_from_email": {
                    "value": "[email protected]",
                    "source-type": "CONTACTS",
                    "source-id": "SOURCE",
                    "source-label": null,
                    "source-vids": [
                        1234
                    ],
                    "timestamp": 1234,
                    "selected": false
                },
                "merged_to_email": {
                    "value": "[email protected]",
                    "source-type": "TYPE",
                    "source-id": "",
                    "source-label": null,
                    "timestamp": 1234,
                    "selected": false
                },
                "first-name": "Example",
                "last-name": "Example"
            }
        ]
    }
],
"has-more": true,
"vid-offset": 1234
}

I don't care about any of the information on here except what is under "properties". Everything in property I need to store in a dictionary so for example <"Country", "United States">. This code below gives me the entire "properties" chuck:

foreach(var item in contacts.Children())
        {
            var itemProps = item.Children<JProperty>();
            var myElement = itemProps.FirstOrDefault(x => x.Name == "properties");
            var myElementValue = myElement.Value;
        }

I;m struggling with how to narrow this down even further and get a dictionary or something filled with the key values that are within properties? Any help would be much appreciated.

2
  • Properties is plural which means it is comprised of multiple values - 8 according to your post - so you need to fish each value out if you are going to parse it Commented Feb 1, 2018 at 22:22
  • We need to know the format of your JSON file to give a concrete answer i.e. a minimal reproducible example. But there are several questions already about parsing huge JSON files without loading them completely into memory, see Deserialize json array stream one item at a time and System.OutOfMemoryException with JSON.NET with List<object>. But loading it with JObject.Parse(json); will maximize memory use since you will allocate a huge string for the JSON and then JProperty string names for all the properties. Commented Feb 1, 2018 at 22:45

1 Answer 1

4

Based on what you've described, your myElement is a JProperty with nested JObjects that each have a JProperty child, like this:

enter image description here

It's a little ugly, and you'll need to add some null checks, etc, but you can walk the element and populate a dictionary of the properties like this:

var myElement = o.Children<JProperty>().FirstOrDefault(x => x.Name == "properties");
var jobj = (JObject)myElement.Value;

Dictionary<string, string> myProperties = new Dictionary<string, string>();

foreach(var jprop in jobj) {

    var  key = jprop.Key;
    var value = ((JObject)jprop.Value).Properties().First().Value.ToString();
    myProperties[key] = value;      
}
Sign up to request clarification or add additional context in comments.

1 Comment

Extremely helpful! Thank you for the visualization of what was going on it really helped me to understand it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.