skip to Main Content

I have 2 json files and i am deserializing them separately in blazor c#, how can i make a join. so i can show country name also in page. eg AN = Netherlands .

json file 1 is below

{"CountryIso2":"AF","CountryName":"Afghanistan"},{"CountryIso2":"AN","CountryName":"Netherlands"}

json file 2 is below

{"Name":"something","Start":"2021-11-10T09:00:00","End":"2021-11-14T09:00:00","AdditionalInformation":"Volufda.</br>","Address":{"Information":"fasdfas","City":"Pelhu0159imov, Vysou010dina","CountryIso2":"AN"}}

my C# code is

public class Event
{
    public string Name { get; set; }

    public DateTime Start { get; set; }

    public DateTime End { get; set; }

    public string AdditionalInformation { get; set; }

    public Addressar Address { get; set; }
}

    public class Addressar
    {
        public string Information { get; set; }
        public string City { get; set; }
        public string CountryIso2 { get; set; }
    }

public class Country
{
    public string CountryIso2 { get; set; }

    public string CountryName { get; set; }
}

give solutions please.

3

Answers


  1. You can use the Newtonsoft.Json.JsonConvert.DeserializeObject method. Start by parsing the json files into Event and Country objects. Then use the Select() method to combine them. For example, you can do something like this:

    public void MergeFiles()
    {
        // Parse the json files into object lists
        var countries = JsonConvert.DeserializeObject<List<Country>>(File.ReadAllText("countries.json"));
        var events = JsonConvert.DeserializeObject<List<Event>>(File.ReadAllText("events.json"));
        
        // Create a new list with the result
        var result = events.Select(x => new {
           Name = x.Name,
           Start = x.Start,
           End = x.End,
           AdditionalInformation = x.AdditionalInformation,
           Address = new {
              Information = x.Address.Information,
              City = x.Address.City,
              CountryIso2 = x.Address.CountryIso2,
              CountryName = countries.First(y => y.CountryIso2 == x.Address.CountryIso2).CountryName
           }
        }).ToList();
        
        // Write the result to file
        File.WriteAllText("Result.txt", JsonConvert.SerializeObject(result);
    }
    

    This will produce a file with json text that contains the events with country names information.

    Login or Signup to reply.
  2. your first json file is invalid , you have to fix it

    [{"CountryIso2":"AF","CountryName":"Afghanistan"},{"CountryIso2":"AN","CountryName":"Netherlands"}]
    

    you can add CountryName property to an Addressar class

    public class Addressar
    {
        public string Information { get; set; }
        public string City { get; set; }
        public string CountryIso2 { get; set; }
        public string CountryName { get; set; }
    }
    

    code

    using System.Text.Json;
    
    Event ev = JsonSerializer.Deserialize<Event>(json2);
      
    List<Country> countries = JsonSerializer.Deserialize<List<Country>>(json1);
      
    ev.Address.CountryName = countries.Where(c=> c.CountryIso2== ev.Address.CountryIso2)
                                      .FirstOrDefault()?.CountryName;
    
    Login or Signup to reply.
  3. As per the existing structure we can apply a where clause and apply the for single results.If we want to go ahead with multiple results we can make use of foreach in this scenario.Please find the complete c# code.You can format it according to your actual result.
     **Your First Json Should be List:**
    **You have to represent like this [ {},{} ]**
    
    **[{'CountryIso2':'AF','CountryName':'Afghanistan'},{'CountryIso2':'AN','CountryName':'Netherlands'}]";**
    
    **Complete C# Code:**
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    
    namespace Assessment
    {
        class P2
        {
            static void Main(string[] args)
            {    
           string countries =
                    "[{'CountryIso2':'AF','CountryName':'Afghanistan'},
                      {'CountryIso2':'AN','CountryName':'Netherlands'}]";
                List<Country> _countries = JsonConvert.DeserializeObject<List<Country>>(countries);
                string eventDetails = 
                    "{'Name':'something','Start':'2021-11-10T09:00:00','End':'2021-11-14T09:00:00',
                     'AdditionalInformation':'Volufda.</br>','Address':
                     {'Information':'Nu00e1drau017enu00ed 1536, Pelhu0159imov',
                         'City':'Pelhu0159imov, Vysou010dina','CountryIso2':'AN'}}";
                Event eventData = JsonConvert.DeserializeObject<Event>(eventDetails);
                eventData.Address.Country = 
                    _countries.Where(p => p.CountryIso2 ==                               eventData.Address.CountryIso2).FirstOrDefault().CountryName;
                Console.WriteLine(JsonConvert.SerializeObject(eventData));
                Console.ReadLine();
            }
        }
    
        public class Event
        {
            public string Name { get; set; }
    
            public DateTime Start { get; set; }
    
            public DateTime End { get; set; }
    
            public string AdditionalInformation { get; set; }
    
            public Addressar Address { get; set; }
        }
    
        public class Addressar
        {
            public string Information { get; set; }
            public string City { get; set; }
            public string CountryIso2 { get; set; }
            public string Country { get; set; }
        }
    
        public class Country
        {
            public string CountryIso2 { get; set; }
    
            public string CountryName { get; set; }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search