Reputation: 285
I have a JSON file and one of the values is represented as a list of integers, but I would like to convert the list to nested JSONS with its value and surrogate key (autoincrement from 1 for each list).
Is it possible to use Newtonsoft.json.dll for this task?
This is actual example
{
"content": {
"table": [
{
"name": {
"en": "questionnaire"
},
"data": [
"154",
"124254",
"87575"
]
}
]
}
}
This is needed output:
{
"content": {
"table": [
{
"name": {
"en": "questionnaire"
},
"data": [
{
"id": "1",
"value": "154"
},
{
"id": "2",
"value": "124254"
},
{
"id": "3",
"value": "87575"
}
]
}
]
}
}
Thank you very much for any ideas.
Upvotes: 0
Views: 1566
Reputation: 4002
You can create a custom converter which is responsible for converting data
array into a new dictionary<string, string>
. The custom converter looks like below:
public class DataConverter : JsonConverter
{
private int _index = 1;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken token = JToken.FromObject(value);
token.WriteTo(writer);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
Dictionary<string, string> dict = new Dictionary<string, string>();
try
{
foreach (var item in token)
{
dict.Add(_index++.ToString(), item.ToString());//Filling the data to dictionary
}
}
catch
{
// ignored
}
_index = 1;//Resetting index
return dict;
}
public override bool CanConvert(Type objectType)
{
return objectType.IsAssignableFrom(typeof(IDictionary<string, string>));
}
}
Then you should have the models as below in order to fetch the json into strongly typed objects. As u can see there is this attribute [JsonConverter(typeof(DataConverter))]
which tells the converter how to serialize/deserialize the content.
[Serializable]
public class Table
{
[JsonProperty("data")]
[JsonConverter(typeof(DataConverter))]
public Dictionary<string, string> Data { get; set; }
}
[Serializable]
public class Content
{
[JsonProperty("table")]
public Table[] Tables { get; set; }
}
[Serializable]
public class MyObject
{
[JsonProperty("content")]
public Content Content { get; set; }
}
Here is a simple console app to test it: - be aware that i removed everything else under table for the sake of simplicity but you can add them in your app.
public static void Main()
{
string content = @"{
""content"": {
""table"": [
{
""data"": [
""154"",
""124254"",
""87575""
]
}
]
}
}";
var result = JsonConvert.DeserializeObject<MyObject>(content);
foreach (var table in result.Content.Tables)
{
Console.WriteLine(JsonConvert.SerializeObject(table));
}
Console.ReadLine();
}
Hope it helps :)
Upvotes: 1
Reputation: 19
If this gives you some idea:
var json = "{'data': ['154','124254','87575']}";
dynamic obj = JsonConvert.DeserializeObject(json);
for (int i = 0; i < obj.data.Count; i++)
{
dynamic val = new { id = i + 1, value = obj.data[i] };
obj.data[i] = JsonConvert.SerializeObject(val);
}
var newJson = JsonConvert.SerializeObject(obj);
Upvotes: 1