JSON converter with uncommon formatting

What Is JSON?

JSON (JavaScript Object Notation) is a very powerful and lightweight data-interchange format. This means that as long as the sender and receiver understand the syntax and format, you can send very lightweight objects back and forth. This format is most commonly used in the web programming space outside of XAML formats (such as SOAP, SAML etc..) for data transportation. As with everything else, people have different opinions of what the standard format is/should be. I won’t be getting into that debate today, just talking about creating a JSON converter. Specifically when retrieving data from an API.

Common Issues with Creating a JSON converter

So what are we going to talk about today? Well…in most instances, developers (good ones), like to follow standards and therefore follow the JSON standards. The reason for this is because when you want others to use your tool (often an API of some sort), you want the developer to easily determine what type of format the data will be coming back in. In most cases, an array of JSON comes back like this:

If you are expecting an object, it would look like this (notice the change from brackets to squiggles).

And to wrap things up when you request multiple objects and not just strings (an array of objects), you get this:

As mentioned above, JSON was designed with web in mind. This means it is  clean, lightweight and very human readable. Have you ever looked at a WSDL from a WCF service? yikes!

So the question is, what do we do when a developer decides to do it a little differently. I say differently because it is still considered valid JSON if you place it into an online JSON converter. The JSON below came from a public UPC API I was using for a phone project a few years ago.

 

Now, there are two major things going on here.

  1. The array of the JSON is encased in squigglies ‘{‘.
  2. There is a “0” and “1” representing the index for the object in the array.

Apparently, you can use the squigglies as an array identifier as long as it is open and closed with the same character. The “0”, although not typical is part of the standard because you are allowed to name each object. This is similar to giving the name of a list to hold it in memory. Personally, I wouldn’t mind if this was the true standard if most people followed it, but most do not and tools like Web API can’t handle it on it’s own very well without a little coaxing.

So what do we do when this happens?

Excellent question! The best thing to start out with is using the third party Newton.Json  .NET libraries to have a fast and simple JSON serializer and deserializer. Once this has been installed, you can use their JSON converter to take data being returned (JSON string) and metamorphose the string into a C# object.

The code to do this would be something like this:

This code creates a new serializer and then uses it’s method called DeserializeObject. This allows you to take the string and deserialize it into an actual object. In this case I was trying to make it dynamic because direct C# objects were failing. It kept telling me that the JSON format was incorrect.

The only solution I figured out was to use JObject.Parse as a token and then using SelectToken to traverse the tree. This seemed like a real reflection hog and hack, so I wasn’t happy with it.

So I posted something to stack overflow here. After discussing my concerns with another posted, and we found something interesting you could use. Dictionary C# objects.

Why does this work? It’s brilliant actually. Because the Dictionary requires 2 types to determine the type the key and value will be, it will take each array object (0,1,2…) and break them out into the C# object and use the integer names of the array as array indexes. Here is what it looks like using the Json.Convert method.

In this example, the string is the index and Foo is the name of the object:

When everything is returned, you will get the following as you would expect ( this is using a tool to make it be easier to read.

Json converter
json converter results

 

Below is exactly what I used originally for reference if the above solution didn’t work.

 

That’s all for now.. Happy coding everyone!

Leave a Reply

Your email address will not be published. Required fields are marked *