43

I'm trying to get Json.Net to serialise a property name without quote marks, and finding it difficult to locate documentation on Google. How can I do this?

It's in a very small part of a large Json render, so I'd prefer to either add a property attribute, or override the serialising method on the class.

Currently, the it renders like this:

"event_modal":
{
    "href":"file.html",
    "type":"full"
}

And I'm hoping to get it to render like: (href and type are without quotes)

"event_modal":
{
    href:"file.html",
    type:"full"
}

From the class:

public class ModalOptions
{
    public object href { get; set; }
    public object type { get; set; }
}
Julian
  • 33,915
  • 22
  • 119
  • 174
Overflew
  • 7,872
  • 10
  • 45
  • 65
  • 4
    That isn't valid JSON. Why do you want to do this? – Marcelo Cantos Sep 26 '11 at 10:09
  • 4
    See [in JSON, Why is each name quoted?](http://stackoverflow.com/questions/2067974/in-json-why-is-each-name-quoted) – Marc Gravell Sep 26 '11 at 10:17
  • 1
    Marcello - It's to meet the needs of a 3rd party jQuery plugin, which feeds in this data. – Overflew Sep 26 '11 at 10:28
  • 2
    @Overflew, can't you fix the plugin to work with JSON properly? – svick Sep 26 '11 at 12:15
  • I don't understand why companies send data like this from webhooks for example? Is it PHP? Or Python generating this invalid JSON? Now I can't receive data because "its invalid" JSON.. I'm sorry.. no offense. just give me my bloody data I aint gonna convince some 3rd party they're idiots! – Piotr Kula Apr 04 '23 at 21:35

2 Answers2

54

It's possible, but I advise against it as it would produce invalid JSON as Marcelo and Marc have pointed out in their comments.

Using the Json.NET library you can achieve this as follows:

[JsonObject(MemberSerialization.OptIn)]
public class ModalOptions
{
    [JsonProperty]
    public object href { get; set; }

    [JsonProperty]
    public object type { get; set; }
}

When serializing the object use the JsonSerializer type instead of the static JsonConvert type.

For example:

var options = new ModalOptions { href = "file.html", type = "full" };
var serializer = new JsonSerializer();
var stringWriter = new StringWriter();
using (var writer = new JsonTextWriter(stringWriter))
{
    writer.QuoteName = false;
    serializer.Serialize(writer, options);            
}
var json = stringWriter.ToString();

This will produce:

{href:"file.html",type:"full"}

If you set the QuoteName property of the JsonTextWriter instance to false the object names will no longer be quoted.

Christophe Geers
  • 8,564
  • 3
  • 37
  • 53
  • 5
    This is actually very handy for working with GraphQL...makes my wonder why the GraphQL guys decided to work with invalid Json. – Eric Nov 01 '18 at 21:15
  • 2
    well, me for once am disappointed that the standard is with double quotes. It makes it unnecessary unreadable and adds nothing, except the outdated concept 'it is valid javascript despite what you put in the property name'. It would have been great if the standard would have been: use double quotes only for the (really rare) special cases (or some other escape character) instead of everything. – Dirk Boer Feb 24 '19 at 12:16
  • 1
    Be careful if your property values are dictionaries. With `QuoteName = false` JSON.NET will happily serialize something like `new {prop = new Dictionary() {{"§$%&:", "myDictEntryValue"}}}` as `{prop:{§$%\u0026::"myDictEntryValue"}}` which is not only invalid JSON but also an invalid JS object literal definition. – user764754 Feb 27 '23 at 18:01
5

You can also try a regex replace, with a substitution, which could handle any serialized object, and replace the quotes for you.

For Example:

var options = new ModalOptions { href = "file.html", type = "full" };
string jsonText = JsonConvert.SerializeObject(options);
string regexPattern = "\"([^\"]+)\":"; // the "propertyName": pattern
Console.WriteLine(Regex.Replace(jsonText, regexPattern, "$1:"));

This would produce:

{href:"file.html",type:"full"}

I built a working web example here. Explanation of regex substitutions are here.