简体   繁体   中英

Transforming JSON from one structure to another

I have a scenario that I am trying to get my head around. I have some JSON that I mostly don't care about except for certain values. I simply want to extract these values and add them to a new object.

Here is the JSON I am starting from:

{
  "name": "Codex JJ",
  "component": {
    "Profile-1": {
      "id": "Profile",
      "type": "Person",
      "attributes": {
                "Hair-color": "blue",
      "Eye-color": "brown",
        "hair-color": "brown",
        "height": "170cm"
      },
      "status": {
        "employed": "true",
        "ethnic": "White"
      }
    },
    "Profile-2": {
      "id": "Profile",
      "type": "Person",
      "attributes": {
                "Hair-color": "blue",
      "Eye-color": "brown",
        "hair-color": "brown",
        "height": "170cm"
      },
      "status": {
        "employed": "true",
        "ethnic": "White"
      }
    }
  }
}

I want to reconstruct it into this JSON:

{
    "name": "NEW JSON"
    "company": [
        {
            "Person": "new-person",
            "attributes": {
                "Hair-color": "blue",
                "Eye-color": "brown",
                "employed": "true",
                "ethnic": "White"
            },
        {
            "Person": "new-person",
            "attributes": {
                "Hair-color": "blue",
                "Eye-color": "brown",
                "employed": "true",
                "ethnic": "White"
            },
        }
    ]
}

What would be the best practice to implement this? Should I use the dynamic keyword to deserialize my incoming JSON? How exactly can I extract values and add them to my created C# objects to generate a whole new JSON structure?

If you just want to transform the JSON and you don't need (or want) a formal object model to represent the data, you could use Json.Net 's LINQ-to-JSON API (JObjects) to do it:

JObject obj = JObject.Parse(json);

JObject newObj = new JObject(
    new JProperty("name", obj["name"]),
    new JProperty("company", new JArray(
        obj["component"]
            .Children<JProperty>()
            .Select(jp => new JObject(
                    new JProperty((string)jp.Value["type"], jp.Name),
                    new JProperty("attributes", jp.Value["attributes"])
                )
            )
        )
    )
);

json = newObj.ToString();

Working demo: https://dotnetfiddle.net/zSWAL0

Here is just another solution using jsonata ( https://www.nuget.org/packages/Retyped.jsonata )

So the expression would be:

{
    "name": "NEW JSON",
    "company": $each($.component, function($v, $k){
    {"Person": "new-person",
    "attributes": {
            "Hair-color": $v.attributes.`Hair-color`,
                "Eye-color": $v.attributes.`Eye-color`,
                "employed": $v.status.employed,
                "ethnic": $v.status.ethnic
        }
    }
    })
}

Live demo: https://try.jsonata.org/6fpDBlo94

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM