a developer's notes – a semi-technical web development BLOG

November 10, 2013

Parsing JSON to Key Value Pairs with JSON.NET

Filed under: C#,JSON — Duy Nguyen @ 8:30 pm
Tags: , , , , , , , , ,

I had to parse JSON into key value pairs recently. The key would be the path of the JSON property. Consider the following JSON:

{
    "car": {
        "type": [{
            "sedan": {
                "make": "honda",
                "model": "civics"
            }
        },
        {
            "coupe": {
                "make": "ford",
                "model": "escort"
            }
        }]
    }
}

For my key/value pair would look this.

Key: car_type_0_sedan_make 
Value: honda

Here is a console application to illustrate what I had to do.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonGetKeyValue
{
    class Program
    {
        public static Dictionary<string, string> jsonKeyValues = new Dictionary<string, string>();

        static void Main(string[] args)
        {

            //JsonDotNetPathProperty();

            string namePrefix = "_myJson";

            string jsonInput = @"
            {
                ""car"": {
                    ""type"": [{
                        ""sedan"": {
                            ""make"": ""honda"",
                            ""model"": ""civic""
                        }
                    },
                    {
                        ""coupe"": {
                            ""make"": ""ford"",
                            ""model"": ""escort""
                        }
                    }]
                }
            }";

            JObject jo = new JObject();

            // Parse json *OBJECT*
            jo = JObject.Parse(jsonInput);

            string paramName = namePrefix;
            jsonKeyValues.Add(paramName, jsonInput);

            ParseJsonProperties(jo, paramName);

            foreach (var item in jsonKeyValues)
            {
                Console.Write("Key: ");
                Console.WriteLine(item.Key);
                Console.Write("Value: ");
                Console.WriteLine(item.Value);
                Console.WriteLine("");
            }

#if DEBUG
            Console.WriteLine("Press any key to close...");
            Console.ReadLine();
#endif

        }

        public static void ParseJsonProperties(JObject jObject, string paramName)
        {
            IEnumerable<JProperty> jObject_Properties = jObject.Properties();

            // Build list of valid property and object types 
            JTokenType[] validPropertyValueTypes = { JTokenType.String, JTokenType.Integer, JTokenType.Float, JTokenType.Boolean, JTokenType.Null, JTokenType.Date, JTokenType.Bytes, JTokenType.Guid, JTokenType.Uri, JTokenType.TimeSpan };
            List<JTokenType> propertyTypes = new List<JTokenType>(validPropertyValueTypes);

            JTokenType[] validObjectTypes = { JTokenType.String, JTokenType.Array, JTokenType.Object };
            List<JTokenType> objectTypes = new List<JTokenType>(validObjectTypes);

            string currentParamName = paramName; //Need to track where we are.

            foreach (JProperty property in jObject_Properties)
            {
                paramName = currentParamName;

                try
                {
                    if (propertyTypes.Contains(property.Value.Type))
                    {
                        ParseJsonKeyValue(property, paramName + "_" + property.Name.ToString());
                    }
                    else if (objectTypes.Contains(property.Value.Type))
                    {
                        //Arrays ex. { names: ["first": "John", "last" : "doe"]}
                        if (property.Value.Type == JTokenType.Array && property.Value.HasValues)
                        {
                            ParseJsonArray(property, paramName);
                        }

                        //Objects ex. { name: "john"}
                        if (property.Value.Type == JTokenType.Object)
                        {
                            JObject jo = new JObject();
                            jo = JObject.Parse(property.Value.ToString());
                            paramName = paramName + "_" + property.Name.ToString();

                            jsonKeyValues.Add(paramName, property.Value.ToString());

                            if (jo.HasValues)
                            {
                                ParseJsonProperties(jo, paramName);
                            }
                            
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            } // End of ForEach
         
            paramName = currentParamName;            

        }

        public static void ParseJsonKeyValue(JProperty item, string paramName)
        {
            jsonKeyValues.Add(paramName, item.Value.ToString());
        }

        public static void ParseJsonArray(JProperty item, string paramName)
        {
            JArray jArray = (JArray)item.Value;

            paramName = paramName + "_" + item.Name.ToString();
            jsonKeyValues.Add(paramName, item.Value.ToString());

            string currentParamName = paramName; //Need track where we are

            try
            {
                for (int i = 0; i < jArray.Count; i++)
                {
                    paramName = currentParamName;

                    paramName = paramName + "_" + i.ToString();
                    jsonKeyValues.Add(paramName, jArray.Values().ElementAt(i).ToString());

                    JObject jo = new JObject();
                    jo = JObject.Parse(jArray[i].ToString());
                    IEnumerable<JProperty> jArrayEnum = jo.Properties();

                    foreach (JProperty jaItem in jArrayEnum)
                    {
                        // Prior to JSON.NET VER 5.0, there was no Path property on JTokens. So we had to track the path on our own.
                        var paramNameWithJaItem = paramName + "_" + jaItem.Name.ToString();

                        var itemValue = jaItem.Value.ToString(Formatting.None);
                        if (itemValue.Length > 0)
                        {
                            switch (itemValue.Substring(0, 1))
                            {
                                case "[":
                                    //Recusion call to itself
                                    ParseJsonArray(jaItem, paramNameWithJaItem);
                                    break;
                                case "{":
                                    //Create a new JObject and parse
                                    JObject joObject = new JObject();
                                    joObject = JObject.Parse(itemValue);

                                    //For this value, reparse from the top
                                    ParseJsonProperties(joObject, paramNameWithJaItem);
                                    break;
                                default:
                                    ParseJsonKeyValue(jaItem, paramNameWithJaItem);
                                    break;
                            }
                        }
                    }
                } //end for loop

                paramName = currentParamName;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public static void JsonDotNetPathProperty()
        {
            string json = @"
            {
                ""car"": {
                    ""type"": [{
                        ""sedan"": {
                            ""make"": ""honda"",
                            ""model"": ""civic""
                        }
                    },
                    {
                        ""coupe"": {
                            ""make"": ""ford"",
                            ""model"": ""escort""
                        }
                    }]
                }
            }";

            JObject obj = JObject.Parse(json);
            JToken token = obj["car"]["type"][0]["sedan"]["make"];
            Console.WriteLine(token.Path + " -> " + token.ToString());
        }

    }
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: