How to handle duplicated properties in JSON

While working with Newtonsoft.Json I needed a way to determine if a JSON string has duplicated properties. Consider the following book object with duplicated title property.

{
"language": "esperanto",
"title": "Primeiro Manual de Esperanto",
"title": "Fundamento de Esperanto",
"author ": "Ludwik Lejzer Zamenhof"
}

To delect duplicates one needs Newtonsoft.Json v.12.0.1 (or greater). At time of writing this post it is the latest and the only version with that feature.

Use JToken.Parse or JToken.Load method and pass in JsonLoadSettings argument with optional DuplicatePropertyNameHandling flag.

Use can also use Parse and Load methods on classes inheriting from JToken: JObject, JArray etc. since the implementation of those 2 methods resides inside the base abstract JToken class.

DuplicatePropertyNameHandling comes in 3 flavours:

Replace (default) – only the last value of a duplicated property is loaded.

{
"language": "esperanto",
"title": "Fundamento de Esperanto",
"author ": "Ludwik Lejzer Zamenhof"
}

Ignore – only the first value of a duplicated property is loaded.

{
"language": "esperanto",
"title": "Primeiro Manual de Esperanto",
"author ": "Ludwik Lejzer Zamenhof"
}

Error – throws a JsonReaderException exception, eg.

Property with the name 'title' already exists in the current JSON object. Path 'title', line 5, position 9.

Check out the following code to see it in action… or take a look at this GitHub project or Gist and play with it on your own.

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DuplicatedJsonProperties
{
	class DuplicatedJsonProperties
	{
		private const string data =
@"{
	""language"": ""esperanto"",
	""title"": ""Primeiro Manual de Esperanto"",
	""title"": ""Fundamento de Esperanto"",
	""author"" : ""Ludwik Lejzer Zamenhof""
}";

		static void Main(string[] args)
		{
			Console.WriteLine("---Input---");
			Console.WriteLine(data);

			PrintJson("Throw exception on duplicates", DuplicatePropertyNameHandling.Error);
			PrintJson("Ignore duplicates", DuplicatePropertyNameHandling.Ignore);
			PrintJson("Replace duplicates with the last property", DuplicatePropertyNameHandling.Replace);

			Console.ReadKey();
		}

		private static void PrintJson(string header, DuplicatePropertyNameHandling duplicateFlag)
		{
			try
			{
				Console.WriteLine();
				Console.WriteLine("---" + header + "---");

				JsonLoadSettings jsonLoadSettings = (duplicateFlag == DuplicatePropertyNameHandling.Replace)
					? null
					: new JsonLoadSettings { DuplicatePropertyNameHandling = duplicateFlag };

				JToken jToken = JToken.Parse(data, jsonLoadSettings);

				Console.WriteLine(jToken.ToString(Formatting.Indented));
				Console.WriteLine();
			}
			catch (JsonReaderException jsonReaderException)
			{
				Console.WriteLine("Exception thrown: " + jsonReaderException.Message);
			}
		}
	}
}

 

NOTE
Setting DuplicatePropertyNameHandling.Replace explicitly results in an error, so in that case I leave the settings blank to bypass this issue (line 36).

UPDATE

The bug was fixed so hopefully there will be no need for a workaround in the next version Newtonsoft.Json (later than v.12.0.1).

Advertisements

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close