How to extract values from json file and rename pdf files with those extract files

Raki 481 Reputation points
2023-03-04T01:39:05.77+00:00

Hello,

i am working on a web application where i need to extract the values from json files from the directory and use those values to rename the pdf files. so there are two things we need to do one is get the values from Json and Check each pdf files by the Insured Information like (Lenox Gardens Apt Corp) and if match with the Company Name then rename that specific pdf file with all extracted values from title. Please have a look @ attached json file as well below screen shot. so basically what ever information in the title has that's the information i would like to use to renaming the pdf file. 001948_Testing.txt

User's image

User's image

Thanks in advanced!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,181 Reputation points Microsoft External Staff
    2023-03-06T08:22:40.12+00:00

    Hi @Raki ,

    First, you need to read the json string using the File.ReadAllText method.

    string allText = System.IO.File.ReadAllText(Server.MapPath("~/Files/Test.txt"));

    Then use the JObject.Parse method: load a JObject from a string containing JSON.

    The JObject.Parse method needs to add a reference: using Newtonsoft.Json.Linq;

            JObject jObject = JObject.Parse(allText);
            var value = jObject["cd6bfb7fe59f48d78ef3cc8012d85abe"]["data"]["Insured Information"]["Company Name"]["value"].ToString();
            string title = jObject.SelectToken("$.cd6bfb7fe59f48d78ef3cc8012d85abe..title").ToString(); 
    

    You can refer to the following demo:

    <asp:Button Text="Download File" runat="server" OnClick="DownloadFile" />

    protected void DownloadFile(object sender, EventArgs e)
    {
        string allText = System.IO.File.ReadAllText(Server.MapPath("~/Files/Test.txt"));
        JObject jObject = JObject.Parse(allText);
        var value = jObject["cd6bfb7fe59f48d78ef3cc8012d85abe"]["data"]["Insured Information"]["Company Name"]["value"].ToString();
        string title = jObject.SelectToken("$.cd6bfb7fe59f48d78ef3cc8012d85abe..title").ToString();                    
        //File to be downloaded.
        string fileName = "Test.pdf";
        if (value == "Lenox Gardens Apt Corp")
        {
            //Set the New File name.
            string newFileName = title;
            //Path of the File to be downloaded.
            string filePath = Server.MapPath(string.Format("~/Files/{0}", fileName));
            //Setting the Content Type, Header and the new File name.
            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + newFileName);
            //Writing the File to Response Stream.
            Response.WriteFile(filePath);
            Response.Flush();
            Response.End();
        }
    }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Zsolt Hajdu 156 Reputation points
    2023-03-04T16:08:29.0166667+00:00

    First you have to create a class (or more) that will contains deserialized data from json file.
    You can use dynamic or ExpandoObject instead of your own class(es).
    After that use Newtonsoft JSON deserialization to that class (install Newtonsoft nuget package).

    Data data = JsonConvert.DeserializeObject<Data>(File.ReadAllText(@"001948_Testing.txt"));
    if (data.InsuredInformation.CompanyName.Value == companyName)
    {
        // rename pdf file
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.