Hello I have this example json string:
{
"property1":"value1",
"property2":"value2",
"anotherObject":{
"property1":"anothervalue1",
"property2":"anothervalue2",
"anotherOfAnother":{
"property1":"value1"
}
}
}
I need to get all others json objects inside and pass then to first level, preserving navigation path. So example Json becomes:
{
"property1":"value1",
"property2":"value2",
"anotherObject.property1":"anothervalue1",
"anotherObject.property2":"anothervalue2",
"anotherObject.anotherOfAnother.Property1":"value1"
}
How can I do it? I'm on .Net 6 Thanks in advance
I'm stuck, I haven't tried nothing yet. I'm looking for ideas on efficient ways to reach the goal, using JObject or JsonConvert.
you can use Path property
var jsonObject = JObject.Parse(json);
var result = jsonObject.Descendants()
.Where(t => !t.HasValues)
.Select(t => "\"" + t.Path + "\"" + " : " + "\"" + t.ToString() + "\",")
.ToArray();
//cut off the last ","
result[result.Length - 1] = result[result.Length - 1]
.Substring(0, result[result.Length - 1].Length - 1);
json = "{\n" + string.Join("\n", result) + "\n}";