none
Read and Update data in Unknown Json Structure in c# RRS feed

  • Question

  • Hi ,

    I am building common component which used to read and update unknown json structure .

    Example:

    1 . docs.fullname    in Example1.Json

    2. Bill.   in Example2.json

    3. Borrowers. PersonalInfo.FirstName in Example3.json

    4 PolicyInfo. BorrowerName in Example4.json

    The every json file has different structure but I need to find the above json path in any where in the json file which need to read and update the value

    My logic here

     JObject jObj = JObject.Parse(jsonData);

    //Reading parent node

    JToken node = jObj.Descendants()

                    .Where(t => t.Type == JTokenType.Property && ((JProperty)t).Name == parentnode(docs, Bill,Borrower,Policyinfo))

                    .Select(p => ((JProperty)p).Value)

                    .FirstOrDefault()

    //Reading child node value  

    //Reading data 

    foreach (JToken tempToken in node.Children())

                    {

                        string nodevalue = (string)tempToken.SelectToken(fullname    or BorrowerFName or Borrowers  );

    }

     

    //updating data

    foreach (JToken tempToken in node.Children())
                    {

    tempToken.SelectToken(fullname or BorrowerFName or Borrowers).Replace(new value)

    }

    Please share if any other good approaches .

    Tuesday, August 11, 2020 4:55 AM

Answers

  • Hi kapil kumar velpuri, 
    About nested json structure, you can use Newtonsoft.Json to parse and  then use SelectToken() to navigate to the data.
    Here is a code example in this link and you can refer to it.
    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Friday, August 14, 2020 9:15 AM

All replies

  • Hi kapil kumar velpuri, 
    You can use JsonConvert.DeserializeObject method to parse json.
    Fisrt, you need to add Json.NET and Newtonsoft.Json references into your project.
    Here is a code example you can refer to.

    //read data
    string json1 = File.ReadAllText(@"C:\Users\Desktop\test.json");
    dynamic jsonObj = JsonConvert.DeserializeObject(json1);
    Console.WriteLine(jsonObj["docs"]);
    //update data
    jsonObj["SongName"] = "new name";
    string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj,Formatting.Indented);
    File.WriteAllText(@"C:\Users\Desktop\test1.json", output);

    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Wednesday, August 12, 2020 5:41 AM
  • Thank you Daniel .

    The above logic is working fine all json structure but not in nested json structure .

     jsonObj["Customers"] is not returning the data.

    {
      "Customers": {
        "Borrowers": [
          {
            "PersonalInfo": {
              "First/Name": null,
              "LastName": null,
              "FirstTimeHomeBuyer": "No"
            }
          }
        ]
      },
      "Products": {
      }
    }

    Thursday, August 13, 2020 11:37 PM
  • Hi kapil kumar velpuri, 
    About nested json structure, you can use Newtonsoft.Json to parse and  then use SelectToken() to navigate to the data.
    Here is a code example in this link and you can refer to it.
    Best Regards,
    Daniel Zhang


    "Windows Forms General" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Windows Forms General" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Friday, August 14, 2020 9:15 AM
  • This question is related to a third party product which we do not provide support for in the MSDN forums. That product has their own support forums available here.

    For Microsoft-supported JSON parsing you should be using System.Text.Json but be aware of the limitations.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, August 14, 2020 1:33 PM
    Moderator
  • Thank you Daniel for sharing valuable information.
    Wednesday, August 26, 2020 4:27 AM