I've got some complex JSON which looks something like this:
{
"memberdetails": [
{
"id": 0,
"label": "General Details",
"visible": true,
"properties": [
{
"label": "Address",
"description": "Residential or Postal Address",
"view": "textarea",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "test 1",
"alias": "address",
"editor": "Umbraco.TextboxMultiple",
"visible": "true"
},
{
"label": "State",
"description": "State of residence",
"view": "textbox",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "test 2",
"alias": "state",
"editor": "Umbraco.Textbox",
"visible": "true"
}
]
},
{
"id": 1,
"label": "Other Details",
"visible": true,
"properties": [
{
"label": "Address",
"description": "Residential or Postal Address",
"view": "textarea",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "test_otherdetails1 ",
"alias": "aliastest",
"editor": "Umbraco.TextboxMultiple",
"visible": "true"
},
{
"label": "State",
"description": "State of residence",
"view": "textbox",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "test_otherdetails2",
"alias": "aliastest2",
"editor": "Umbraco.Textbox",
"visible": "true"
}
]
},
{
"id": 2,
"label": " Details",
"visible": true,
"properties": [
{
"label": "Address",
"description": "Residential or Postal Address",
"view": "textarea",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": " Details1",
"alias": "Details1Test",
"editor": "Umbraco.TextboxMultiple",
"visible": "true"
},
{
"label": "State",
"description": "State of residence",
"view": "textbox",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "Details2",
"alias": "Details12est",
"editor": "Umbraco.Textbox",
"visible": "true"
}
]
}
]
}
I'm trying to extract the alias
and value
items from the JSON and put them into a single JObject like this:
{
"address": "test 1",
"state": "test 2",
"aliastest": "test_otherdetails1 ",
"aliastest2": "test_otherdetails2",
"Details1Test": " Details1",
"Details12est": "Details2"
}
Is there an easy way to do using Json.Net? Note the alias
values will all be distinct in the actual JSON.
Yes, you can transform your JSON like this:
JObject obj = JObject.Parse(json);
JObject result = new JObject(
obj["memberdetails"]
.SelectMany(jt => jt["properties"])
.Select(jt => new JProperty((string)jt["alias"],jt["value"]))
);
Console.WriteLine(result.ToString());
Fiddle: https://dotnetfiddle.net/pyAxhu
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.