Grant Kochmann
Grant Kochmann

Reputation: 179

Get child properties from JSON using Json.NET and C#

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.

Upvotes: 1

Views: 11987

Answers (1)

John Wright
John Wright

Reputation: 4607

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;      
}

Upvotes: 4

Related Questions