none
How to convert one Json data type to another Json data type. RRS feed

  • Question

  • I have one type of Json data type in string, and I want to convert it to another Json data type using built-in System.Text.Json.

    The following is the C# class for Json data type and the source Json string and target Json string.

        public class Student
        {
            public string username { get; set; }
            public string run_type { get; set; }
            public string school_code { get; set; }
        }
    
        public class RootSource
        {
            public List<Student> Students { get; set; }
        }
    
        public class StudentObject
        {
            public List<List<string>> Students { get; set; }
        }
    
    string json_root_source1 = @"{""Students"": [{ ""username"":""John"",""run_type"":""100M"",""school_code"":""ibc""},{ ""username"":""Smith"",""run_type"":""100M"",""school_code"":""sbo""}]}";
    string json_root_target1 = @"{""Students"": [{ ""ibc"" : ""John""},{ ""sbo"":""Smith""}]}";

    Basically, I want to covert this string

    {"Students": [{ "username":"John","run_type":"100M","school_code":"ibc"},{ "username":"Smith","run_type":"100M","school_code":"sbo"}]}";

    To this string:

    {"Students": [["ibc", "John"],[ "sbo","Smith"]]}

    The target string will contain the school_code plus the username.

    Then I will use the target string in another more complicate Json data structure.

    I have to use .NET core 3.0, with Visual Studio 2019 Version 16.3.10 on Windows 10 Version 1909.

    Please advice!

    Thursday, November 21, 2019 2:48 PM

Answers

  • In this case, try:

    public class RootDestination

    {

       public List<string[]> Students { get; set; }

    }

    . . .

    RootDestination dest = new RootDestination

    {

       Students = source.Students

                    .Select( s => new[] { s.school_code, s.username } )

                    .ToList( )

    };

     


    • Edited by Viorel_MVP Friday, November 22, 2019 12:35 PM
    • Marked as answer by zydjohn Friday, November 22, 2019 2:14 PM
    Friday, November 22, 2019 12:34 PM

All replies

  • Try this:

    using System.Linq;

    . . .

    public class RootDestination

    {

       public List<Dictionary<string, string>> Students { get; set; }

    }

    . . .

    RootSource source = JsonSerializer.Deserialize<RootSource>( json_root_source1 );

     

    RootDestination dest = new RootDestination

    {

       Students = source.Students

          .Select( s => new Dictionary<string, string> { { s.school_code, s.username } } )

          .ToList( )

    };

     

    string result = JsonSerializer.Serialize( dest );

    Thursday, November 21, 2019 5:23 PM
  • Hello:

    Thanks for your code, your code works.  But I found my example gave the wrong target object.

    The following is my target object:

    string json_root_target1 = @"{""Students"": [[ ""ibc"" : ""John""],[ ""sbo"":""Smith""]]}";

    All other data is the same.

    I want to change your code to get the target, but it seems the C# class definition is not correct for the target object.

    Any advice?

    Friday, November 22, 2019 12:13 PM
  • In this case, try:

    public class RootDestination

    {

       public List<string[]> Students { get; set; }

    }

    . . .

    RootDestination dest = new RootDestination

    {

       Students = source.Students

                    .Select( s => new[] { s.school_code, s.username } )

                    .ToList( )

    };

     


    • Edited by Viorel_MVP Friday, November 22, 2019 12:35 PM
    • Marked as answer by zydjohn Friday, November 22, 2019 2:14 PM
    Friday, November 22, 2019 12:34 PM