Hello I have a dictionary that I am using.
Dictionary<string, string> myProperties
I am trying to format this into a JSON and am having difficulties. I need it to be like this:
{
"properties": [
{
"property": "firstname",
"value": "John"
},
{
"property": "lastname",
"value": "Doe"
},
{
"property": "country",
"value": "united states"
}
]
}
Currently I am using Json.NET to serialize the dictionary but that gives me this:
{
"country": "united states",
"firstname": "John",
"lastname": "Doe"
}
Can anybody help me format this into what I need. Any help would be much appreciated.
You get there by sending the result from .Select() to the json serializer wrapped in a anon class, but I would suggest that you use real classes if you intend to build something larger.
JsonConvert.SerializeObject(
new {properties = myProperties.Select(kv => new { property = kv.Key, value = kv.Value})}
,Formatting.Indented);
This will give you
{
"properties": [
{
"property": "firstname",
"value": "John"
},
{
"property": "lastname",
"value": "Doe"
},
{
"property": "country",
"value": "united states"
}
]
}
Building on N0b1ts answer:
JsonConvert.SerializeObject(new { properties = myProperties.Select(kvp => new { property = kvp.Key, value = kvp.Value }).ToList() }, Formatting.Indented);
Related
We have json format as shown below and want to normalize it as given in expected output.
Input format:
[
{
"country": "Germany",
"name": "2010",
"value": 40632
},
{
"country": "United States",
"name": "2010",
"value": 0
},
{
"country": "United States",
"name": "2000",
"value": 45986
},
{
"country": "United States",
"name": "1990",
"value": 37060
},
{
"country": "France",
"name": "2010",
"value": 36745
},
{
"country": "France",
"name": "2000",
"value": 34774
}
]
Expected output :
[
{
"name": "Germany",
"series": [
{
"name": "2010",
"value": 40632
}
]
},
{
"name": "United States",
"series": [
{
"name": "2010",
"value": 0
},
{
"name": "2000",
"value": 45986
},
{
"name": "1990",
"value": 37060
}
]
},
{
"name": "France",
"series": [
{
"name": "2010",
"value": 36745
},
{
"name": "2000",
"value": 34774
}
]
}
]
try this
var jArr = JArray.Parse(input);
var groupedData = jArr.GroupBy(a => a["country"]).ToList();
var outputArr = new JArray();
foreach (var item in groupedData)
{
JObject obj = new JObject();
obj.Add("name", item.Key);
var arr = new JArray();
obj.Add("series", arr);
foreach (var jObj in item)
{
JObject newObj = new JObject();
newObj.Add("name", jObj["name"]);
newObj.Add("value", jObj["value"]);
arr.Add(newObj);
}
outputArr.Add(obj);
}
var output = outputArr.ToString();
This question already has answers here:
C# JSON Serialization of Dictionary into {key:value, ...} instead of {key:key, value:value, ...}
(6 answers)
Closed 2 years ago.
I am trying to serialize the following data structure to JSON:
public class TestClass {
public TestClass() {
Translations = new List<Dictionary<string, Dictionary<string, string>>>();
}
[JsonProperty("translations")]
public List<Dictionary<string, Dictionary<string, string>>> Translations { get; set; }
}
The result json string should look like this:
„translations“: [
„xy“: {
„de-DE“: „Kommando1“,
„en-US“: „Command1“,
(…)
},
„ab“: {
„de-DE“: „Kommando2“,
„en-US“: „Command2“,
(…)
}
]
But instead, this is the output currently:
"translations": [
[
{
"Key": "action1",
"Value": [
{
"Key": "de-DE",
"Value": "Aktion 1 durchgeführt"
}
]
}
],
[
{
"Key": "Aktion2",
"Value": [
{
"Key": "cz-CZ",
"Value": "Zahajit vymenu "
},
{
"Key": "de-DE",
"Value": "Aktion2 durchführen"
},
{
"Key": "en-US",
"Value": "Execute action2"
},
{
"Key": "fr-FR",
"Value": "EXECUTER E Actione"
}
]
}
],
[
{
"Key": "Action3",
"Value": [
{
"Key": "cz-CZ",
"Value": "Vytvorit na vycisteni"
},
{
"Key": "de-DE",
"Value": "Aktion3 generieren"
},
{
"Key": "en-US",
"Value": "Action3 creation"
},
{
"Key": "fr-FR",
"Value": "GENERER MISSION"
}
]
}
], (...)
I would like to know how to serialize the dictionaries without the "key" and "value" strings, but with the values directly (like in the example above): So "en-US": "command2" instead of "key": "en-US", "value": "command2". If this is not possible using dictionary, then I would like to know which container to use in order to achieve this format.
I am writing an answer now because I got it working without any necessary work:
I get the following json-output:
[
{
"first": {
"de-De": "test1",
"de-De1": "test2",
"de-De2": "test3"
},
"second": {
"en-US": "test1us",
"en-US1": "test2us",
"en-US2": "test3us"
}
}
]
By simply doing this:
var Translations = new List<Dictionary<string, Dictionary<string, string>>>();
var dic = new Dictionary<string, string>
{
{"de-De","test1" },
{"de-De1","test2" },
{"de-De2","test3" },
};
var dic2 = new Dictionary<string, string>
{
{"en-US","test1us" },
{"en-US1","test2us" },
{"en-US2","test3us" },
};
var maindic = new Dictionary<string, Dictionary<string, string>>();
maindic.Add("first", dic);
maindic.Add("second", dic2);
Translations.Add(maindic);
var output = JsonConvert.SerializeObject(Translations, Formatting.Indented);
EDIT:
As #Selvin mentioned, it seems like you do not need the List<...> since the listed dictionaries already contain the keys you want to use. So I would go with:
var Translations = new Dictionary<string, Dictionary<string, string>>();
var dic = new Dictionary<string, string>
{
{"action1","test1" },
{"action2","test2" },
{"action3","test3" },
};
var dic2 = new Dictionary<string, string>
{
{"action1","test1us" },
{"action2","test2us" },
{"action3","test3us" },
};
Translations.Add("de-DE", dic);
Translations.Add("en-US", dic2);
var output = JsonConvert.SerializeObject(Translations, Formatting.Indented);
which ultimately leads to:
{
"first": {
"de-De": "test1",
"de-De1": "test2",
"de-De2": "test3"
},
"second": {
"en-US": "test1us",
"en-US1": "test2us",
"en-US2": "test3us"
}
}
I have a problem deserializing a json file, this is the json:
[
{
"id": "id",
"number": "48",
"date": "17-01-2020",
"details": [
{
"id": "id",
"code": "code",
"description": "desc"
},
{
"id": "id",
"code": "code",
"description": "desc"
}
],
"address": "add",
"note": null
},
{
"id": "id",
"number": "55",
"date": "17-01-2020",
"details": [
{
"id": "id",
"code": "code",
"description": "desc"
},
{
"id": "id",
"code": "code",
"description": "desc"
}
],
"address": "add",
"note": null
}
]
this is my code:
var result = httpClient.GetAsync(".....").Result;
List<Docu> doc= new JavaScriptSerializer().Deserialize<List<Docu>>(result.Content.ReadAsStringAsync().Result);
class Docu contains definition of id, number, date, details and:
public List<Details> det{ get; set; }
Class Details contains id, code and description definition
I can deserialize everything except complex object details, it returns null from deserialization, how I can fix this? I need to fill the list of details
You have wrong name for List<Details> property
it should be
public List<Details> details{ get; set; }
according to json you have shown
This question already has answers here:
Remove duplicates in the list using linq
(11 answers)
Closed 2 years ago.
I am very new to LINQ. I have run to a scenario where I need to find distinct from an object and remove the others. Please help.
I have tried like this
var xyz = referal.GroupBy(i => i.Select(x => x.identifier).Distinct().Select(g=>g));
but it is not returning the distinct values. Thanks in advance.
Sample Object :
{
"referral_type": [
{
"type": "MOBILE",
"invitee": [
{
"identifier": "12345678",
"name": "Test2",
"invited_on": "2020-01-29 12:25:46.0",
"till": {
"code": "",
"name": ""
}
},
{
"identifier": "98765432",
"name": "Test1",
"invited_on": "2020-01-29 13:37:36.0",
"till": {
"code": "",
"name": ""
}
},
{
"identifier": "12345678",
"name": "Harry Test",
"invited_on": "2020-01-29 13:55:32.0",
"till": {
"code": "",
"name": ""
}
},
{
"identifier": "98765432",
"name": "Harry Test",
"invited_on": "2020-01-29 13:55:32.0",
"till": {
"code": "",
"name": ""
}
}
]
}
]
}
So I want to check the distinct identifier and take the first one irresoective of other values, something like this
{
"referral_type": [
{
"type": "MOBILE",
"invitee": [
{
"identifier": "12345678",
"name": "Test2",
"invited_on": "2020-01-29 12:25:46.0",
"till": {
"code": "",
"name": ""
}
},
{
"identifier": "98765432",
"name": "Test1",
"invited_on": "2020-01-29 13:37:36.0",
"till": {
"code": "",
"name": ""
}
}
]
}
]
}
You need to implement the interface IEquatable<T> class "invitee", because Dinstinct use this to compare object. On this way i think you are comparing memory references.
I have a JArray of JArrays, but I would like to flatten it into a single JArray of JObjects. I have already implemented a foreach loop which iterates through each JArray in my JArray. I need to know how to flatten each sub-JArray into a JObject.
Here is an example:
[
{
"item": [
{
"fieldName": "Name",
"value": "Andy"
},
{
"fieldName": "Phone",
"value": "678-905-9872"
}
]
},
{
"item": [
{
"fieldName": "Name",
"value": "John"
},
{
"fieldName": "Phone",
"value": "688-954-5678"
}
]
},
{
"item": [
{
"fieldName": "Name",
"value": "Ashley"
},
{
"fieldName": "Phone",
"value": "+44 671 542 8945"
}
]
},
{
"item": [
{
"fieldName": "Name",
"value": "Avi"
},
{
"fieldName": "Phone",
"value": "(212)-908-7772"
}
]
}
]
I would like each item to be a single JObject, resulting in the following JArray:
[
{
"Name": "Andy"
"Phone": "678-905-9872"
},
{
"Name": "John"
"Phone": "688-954-5678"
{
"Name": "Ashley"
"Phone": "+44 671 542 8945"
},
{
"Name": "Avi"
"Phone": "(212)-908-7772"
}
]
Thanks!
EDIT
Here is my solution (c#, using Newtonsoft.Json)
public string ParserFunction(string json)
{
string fieldname, fieldvalue;
JArray jsonArray = JArray.Parse(json);
foreach (JObject item in jsonArray)
{
JArray temp = (JArray)item["columns"]; //create new temporary JArray
foreach (JObject jobject in temp)
{
fieldname = jobject["fieldName"].ToString();
fieldvalue = jobject["value"].ToString();
item.Add(fieldname, fieldvalue);
jobject.Remove("fieldName");
jobject.Remove("value");
}
item.Remove("item");
}
json = jsonArray.ToString();
return json;
}
Not sure if this is the most optimal way to do it, I saw an answer below which looks alright as well.
var jArr = new JArray(JArray.Parse(JSON)
.Select(x => new JObject(new JProperty("Name", x["item"][0]["Name"]),
new JProperty("Phone", x["item"][1]["Phone"])
)
)
);
var str = JsonConvert.SerializeObject(jArr, Formatting.Indented);
str would be:
[
{
"Name": "Andy",
"Phone": "(785) 241-6200"
},
{
"Name": "Arthur Song",
"Phone": "(212) 842-5500"
},
{
"Name": "Ashley James",
"Phone": "+44 191 4956203"
},
{
"Name": "Avi Green",
"Phone": "(212) 842-5500"
}
]