JSON.NET append to existing key

twitter logo github logo ・1 min read

A quick example for appending to an existing key in a JSON.NET object, since for me at least it wasn't clear in the documentation.

JObject myJson = new JObject(new JProperty("errors", new JObject()));

/// we can now reference it as follows just match the type

myJson["errors"].Value<JObject>().Add(new JProperty("title", title));

Small update:

You can even add multiple entries to an JArray the same way

/// assuming errors is a JArray, this will automatically append new entries
ErrorReponse["errors"].Value<JArray>().Add(new JObject(new JProperty("title", title)));
twitter logo DISCUSS
Classic DEV Post from May 18

Aim to Never Stop Learning

It's OK to not know everything. It's OK to be wrong.

Alexandru Bucur profile image
Programmer, Manager and a jack of all trades. Making things work since the dawn of time.

Like what you're reading?
Hit the +FOLLOW button.

❤️