Exercise: Drone Telemetry Processor

Parse dynamic JSON data and map it to an expandable object with custom behavior.

Problem statement

You are developing a telemetry dashboard for racing drones. The drone transmits its current state as a lightweight JSON payload. You need to process this data on the fly and compute derived metrics without defining rigid class structures.

Task requirements

  • Parse the provided JSON string into a dynamic node.

  • Extract the motorSpeed and windResistance values from the JSON data.

  • Create a new expandable object and dynamically assign the extracted values to it as properties.

  • Attach a method to the expandable object that calculates the estimated horizontal velocity. The formula is (motorSpeed / 100.0) - windResistance.

  • Execute the dynamic method and print the calculated velocity to the console.

Constraints

  • Use JsonNode.Parse and string indexers to extract the JSON values dynamically.

  • Use the ExpandoObject class to create the dynamic telemetry object.

  • Use a Func<double> delegate to define and attach the dynamic velocity calculation method.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Declare your parsed JSON node using the dynamic keyword.

  • Use string indexers like node["motorSpeed"] to retrieve values from the parsed JSON node.

  • When defining the Func<double> delegate, you can access the properties you just added to the ExpandoObject directly inside the lambda expression.

Exercise: Drone Telemetry Processor

Parse dynamic JSON data and map it to an expandable object with custom behavior.

Problem statement

You are developing a telemetry dashboard for racing drones. The drone transmits its current state as a lightweight JSON payload. You need to process this data on the fly and compute derived metrics without defining rigid class structures.

Task requirements

  • Parse the provided JSON string into a dynamic node.

  • Extract the motorSpeed and windResistance values from the JSON data.

  • Create a new expandable object and dynamically assign the extracted values to it as properties.

  • Attach a method to the expandable object that calculates the estimated horizontal velocity. The formula is (motorSpeed / 100.0) - windResistance.

  • Execute the dynamic method and print the calculated velocity to the console.

Constraints

  • Use JsonNode.Parse and string indexers to extract the JSON values dynamically.

  • Use the ExpandoObject class to create the dynamic telemetry object.

  • Use a Func<double> delegate to define and attach the dynamic velocity calculation method.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Declare your parsed JSON node using the dynamic keyword.

  • Use string indexers like node["motorSpeed"] to retrieve values from the parsed JSON node.

  • When defining the Func<double> delegate, you can access the properties you just added to the ExpandoObject directly inside the lambda expression.

C# 14.0
using System.Text.Json.Nodes;
using System.Dynamic;
string telemetryJson = """{ "altitude": 150, "windResistance": 12.5, "motorSpeed": 4500 }""";
// Parse the JSON string into a dynamic node
// Create the ExpandoObject and assign extracted properties
// Add the dynamic method using a Func<double> delegate
// Call the method and print the result