JSON: A Simple, But Powerful Way of Dealing with Objects

JSON is really easy once you get the hang of it, and incredibly powerful.

JSON Logo

I started using JSON a while back when making my site’s Trakt API, but only recently started using it with a VB .Net program I was writing. It was so incredibly easy to use, and somewhat fun, that I wanted to talk a little about how to use JSON data when working with VB.Net.

JSON stands for “JavaScript Object Notation”. It is a way of storing objects using a very strict notation format as a string. As you can probably guess, JSON is primarily used in Javascript (which has built-in routines for parsing and reading JSON arrays). However, you can easily find libraries which you can download and incorporate into your own projects that will allow you to use JSON with your applications. The main purpose of this post is to talk about how to use them.

What makes JSON so easy to use is that you’d give it a string like below:

JSON String

Using a built in Javascript routine, you can easily parse the above string to a JSON array. Below is is what the parsed JSON array would look like (colored for ease of reading):

JSON Array

In the example above, the array contains two JSON objects. Each object has two properties, a name and a birthdate. The name property has two sub-properties, a first name and a last name.

This type of functionality is very useful if you are able to capture data stored as text strings. Sometimes, you may require this functionality for VB.Net. Unfortunately, a JSON parser for VB.Net is not built in (as far as I’m aware), and so, you will need to use external libraries to get the functionality you need. So, I used Newtonsoft’s JSON .Net, which is available for download for free over here. What I’d like to do is cover how to use these libraries in your own VB.Net projects. Particularly, I’d like to cover how to handle strings that are being imported from other sources as a JSON string. That is, you don’t have an object ready to cast the string into. If you did have an object created in your code, the process becomes a lot easier, but I won’t be

Once you’ve downloaded the library, you’ll need to first import the correct files into your project before you can use them. To do this, navigate to the “Bin” subfolder in the archive and extract all the files to your project’s working directory. Inside Visual Studio, right click your project and then click “”Add Reference”. Navigate and select the DLL file you just extracted. If done correctly, you’ll be able to call functions from within this DLL.

There are lots of different things you can do with JSON data, too many to list out. But here are a few of the more common tasks you may be required to carry out using data. I’ll use the JSON data I put above for consistency.

Start by importing the respective namespace for ease:
JSON Import

Importing JSON data from a string:
JSON Deserialize

 

Converting a JSON object back to a string:
JSON Serialize

 

Check if a property exists:
JSON Exists

 

Change the value of a property:
JSON Change Value

Looping Through a JSON object:
JSON Loop 1 Deep


Looping Through a JSON object (works exactly like an array):
JSON Loop 2 Deep

 

Removing an item from a JSON object:
JSON Remove Full

Before After
JSON Array JSON Remove Full Result

Removing property from an item:
JSON Remove Partial

 

Before After
JSON Array JSON Remove Partial Result

 

Adding a JSON item:
JSON Add Full

 

Before After
JSON Array JSON Add Full Result

 

Adding a property to an item:
JSON Add Property

 

Before After
JSON Array JSON Add Property Result

 

Adding a property with children to an item:
JSON Add Property Chain

 

 

Before After
JSON Array JSON Add Property Chain Result

I hope this post serves as a little help for people that want to get into using the Newtonsoft JSON library from the .Net framework, particularly for Visual Studio. A lot of the guides and forum posts online target C#, which has a slightly different syntax when dealing with objects, which may make it a little difficult to follow, especially for an amateur programmer. I had a problem learning this myself as well.

If there are any inaccuracies, mistakes, or ways I could better achieve what I was doing in the code above, please do let me know. I’m not an expert programmer, by any means, so what seems correct to me may not be the best or correct way of doing something.

This entry was posted in Yay. Bookmark the permalink.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.