You can create a custom JsonConverter to translate between two array formats:
class CustomArrayConverter<T> : JsonConverter { string PropertyName { get; set; } public CustomArrayConverter(string propertyName) { PropertyName = propertyName; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JArray array = new JArray(JArray.Load(reader).Select(jo => jo[PropertyName])); return array.ToObject(objectType, serializer); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { IEnumerable<T> items = (IEnumerable<T>)value; JArray array = new JArray( items.Select(i => new JObject( new JProperty(PropertyName, JToken.FromObject(i, serializer))) ) ); array.WriteTo(writer); } public override bool CanConvert(Type objectType) {
To use the converter, mark the array property with the [JsonConverter] attribute, as shown below. Please note: the type parameter for the converter must match the type of the array element, and the second attribute parameter must be the name of the property that will be used for values ββin the JSON array.
class Foo { [JsonProperty("ids")] [JsonConverter(typeof(CustomArrayConverter<int>), "id")] public int[] MyIds { get; set; } }
Here is a roundabout demo: https://dotnetfiddle.net/vUQKV1
source share