I need to merge an array of objects into a single one, based on the items values The idea is more or less like this: I have this C# method
private SecurityResponse mergeSecuritySettings(SecurityResponse[] profileDetails)
{
// return merged object
}
in which SecurityResponse[] is an array of security profiles, each one with his own security settings for each item on the array e.g.:
[
{
"colorSettings": [
{
"settingKey": "A_288",
"securitySettingValue": "R"
},
{
"settingKey": "A_288_556",
"securitySettingValue": "R"
}
],
"speedSettings": [
{
"settingKey": "L_19",
"securitySettingValue": "R"
},
{
"settingKey": "L_19_38",
"securitySettingValue": "H"
}
],
"shapeSettings": [
{
"settingKey": "P_8",
"securitySettingValue": "W"
},
{
"settingKey": "P_8_8",
"securitySettingValue": "W"
}
],
"brandSettings": [
{
"settingKey": "R_340",
"securitySettingValue": "R"
},
{
"settingKey": "R_337",
"securitySettingValue": "H"
}
]
},
{
"colorSettings": [
{
"settingKey": "A_288",
"securitySettingValue": "W"
},
{
"settingKey": "A_288_556",
"securitySettingValue": "W"
}
],
"speedSettings": [
{
"settingKey": "L_19",
"securitySettingValue": "R"
},
{
"settingKey": "L_19_38",
"securitySettingValue": "W"
}
],
"shapeSettings": [
{
"settingKey": "P_8",
"securitySettingValue": "W"
},
{
"settingKey": "P_8_8",
"securitySettingValue": "W"
}
],
"brandSettings": [
{
"settingKey": "R_340",
"securitySettingValue": "R"
},
{
"settingKey": "R_337",
"securitySettingValue": "R"
}
]
}
]
the key here is the "securitySettingValue" (W = Write, R = Read, H = Hide) , and the output criteria should be from the most... up to the less permissive (notice in each item there is a unique settingKey that will repeat on each element of the "SecurityResponse" array),
so... for he input data from above, the output should be:
{
"colorSettings": [
{
"settingKey": "A_288",
"securitySettingValue": "W"
},
{
"settingKey": "A_288_556",
"securitySettingValue": "W"
}
],
"speedSettings": [
{
"settingKey": "L_19",
"securitySettingValue": "R"
},
{
"settingKey": "L_19_38",
"securitySettingValue": "W"
}
],
"shapeSettings": [
{
"settingKey": "P_8",
"securitySettingValue": "W"
},
{
"settingKey": "P_8_8",
"securitySettingValue": "W"
}
],
"brandSettings": [
{
"settingKey": "R_340",
"securitySettingValue": "R"
},
{
"settingKey": "R_337",
"securitySettingValue": "R"
}
]
}
Maybe the solution is simple, but I'm not seeing it without entering in several nested loops, (mainly because of my lack of knowledge in C#).
Any suggestion on how to approach this one ?, are there built in methods for this ?
Thanks in advance!
Try like this:
Use
Serialize JSONandDeserialize JSON, look at https://www.newtonsoft.com/jsonResult: