1

I am serializing an anonymous object to use as a request message for an HTTP post request. The problem is that one of the JSON keys contains a dot in its name. VS throws out ''invalid anonymous type member declarator'' error.

return JsonConvert.SerializeObject(new
{
    query = "something",
    firstname.keyword = "xyz"
});

What can I do to solve this issue?

Edit: the real json request looks something like this, so I don't think I can use a dictionary:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "firstname.keyword": ""
          }
        }
      ],
      "must_not": [ ],
      "should": [ ]
    }
  },
  "from": 0,
  "size": 10,
  "sort": [ ],
  "aggs": { }
}
m1547
  • 77
  • 8

1 Answers1

6

Json can generally be represented using arrays, dictionaries and anonymous objects.

The first part of your Json can be generated as follows:

return JsonConvert.SerializeObject(new
{
    query = new
    {
        @bool = new
        {
            must = new[]
            {
                new
                {
                    term = new Dictionary<string, object>
                    {
                        ["firstname.keyword"] = string.Empty,
                    }
                }
            }
        }
    }
});
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120