Parse JSON with non-string key values

Is it possible to parse JSON with JSONSerialization and have a hash of [AnyHashable:Any] instead of [String:Any]? I would guess the ability to do this is with the readingOptions but I cannot find any good documentation on this anywhere.

The example payload I have would be:

{
  0: [1,2,3],
  1: "foobar",
  2: {
    0: 1
  }
  "a": 123
}

So the resulting key types are restricted to just String and Int but the value types could be String, Int, Array<Any>, and [AnyHashable:Any]

The format you are describing is not JSON. JSON requires string keys and always has.

4 Likes