DEV Community

Cover image for Import specific properties from JSON
Karen Payne
Karen Payne

Posted on

3 2 2 2 1

Import specific properties from JSON

Introduction

Learn how to read a json string with known properties and remove unwanted properties, followed by deserializing to a strongly typed class.

From incoming to desired data type

For this demonstration, the following data is incoming data with an extra property, Age, and two properties, Name and Last, which need to be FirstName and LastName.

[
  {
    "Id": 1,
    "Name": "Mary",
    "Last": "Jones",
    "Age": 22
  },
  {
    "Id": 2,
    "Name": "John",
    "Last": "Burger",
    "Age": 44
  },
  {
    "Id": 3,
    "Name": "Anne",
    "Last": "Adams",
    "Age": 33
  },
  {
    "Id": 4,
    "Name": "Paul",
    "Last": "Smith",
    "Age": 29
  },
  {
    "Id": 5,
    "Name": "Lucy",
    "Last": "Brown",
    "Age": 25
  }
]
Enter fullscreen mode Exit fullscreen mode

The class to import data into.

public class Person
{
    public int Id { get; set; }
    [JsonPropertyName("Name")]
    public string FirstName { get; set; }
    [JsonPropertyName("Last")]
    public string LastName { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Aliasing properties

JsonPropertyName is used to alias from property names in json to what is in the class.

Removing unwanted properties

This is a simple process, read in a json file with the correct format followed iterating the array and for each item remove one or more properties. Here only one property is being removed.

var jsonArray = JsonNode.Parse(File.ReadAllText("peopleIncoming.json"))!.AsArray();

foreach (var item in jsonArray)
{
    JsonObject obj = item!.AsObject();
    obj.Remove("Age");
}
Enter fullscreen mode Exit fullscreen mode

Next, place the modified json into a variable and write the json to a file.

var updatedJson = jsonArray.ToJsonString(Indented);

DisplayUpdatedJsonPanel(updatedJson);

Console.WriteLine();

File.WriteAllText("People.json", updatedJson);
Enter fullscreen mode Exit fullscreen mode

Indented definition

public static JsonSerializerOptions Indented => new() { WriteIndented = true };
Enter fullscreen mode Exit fullscreen mode

Deserializing to the Person class

var people = JsonSerializer.Deserialize<Person[]>(updatedJson);
Enter fullscreen mode Exit fullscreen mode

The following screenshot is from a sample project that is included.

Output from sample project

Summary

With the provided instructions, it is easy to import json data into a desired format.

See also

C# System.Text.Json

Source code

Google AI Education track image

Build Apps with Google AI Studio 🧱

This track will guide you through Google AI Studio's new "Build apps with Gemini" feature, where you can turn a simple text prompt into a fully functional, deployed web application in minutes.

Read more →

Top comments (1)

Collapse
 
michael_liang_0208 profile image
Michael Liang

Helpful post!

Google AI Education track image

Build Apps with Google AI Studio 🧱

This track will guide you through Google AI Studio's new "Build apps with Gemini" feature, where you can turn a simple text prompt into a fully functional, deployed web application in minutes.

Read more →

👋 Kindness is contagious

Explore this insightful piece, celebrated by the caring DEV Community. Programmers from all walks of life are invited to contribute and expand our shared wisdom.

A simple "thank you" can make someone’s day—leave your kudos in the comments below!

On DEV, spreading knowledge paves the way and fortifies our camaraderie. Found this helpful? A brief note of appreciation to the author truly matters.

Let’s Go!