Read Json Array in Azure functions using C#

Pavan G 186 Reputation points
2021-07-23T06:32:48.143+00:00

Hi Experts,

I am very new to C# and Azure functions. I am trying to read a JSON message from a Kafka service using Azure function. Requirement is to read the JSON file and create CSV file out of it, load it to DataLake and DataBase.

I am able to read the JSON when the JSON doesn't have any array's in it. I am facing issue when I get array of consigmentes for one header ID. We may get multiple consignments for one OrderID.That time we have to create multiple records considering consigmentID.
Any leads or code to read JSON array data will be really helpful.

Below are the details:

C# code to read JSON to from a CSV file:

string sb = json.header.orderID.ToString() + "," + json.consignments[0].consignmentID.ToString()+ "," + json.consignments[0].shippingAddress.postalCode.ToString() + "," + json.header.country.ToString() + "," + json.consignments[0].warehouseID.ToString()

Input Data:

{
   "header":{
      "orderID":"008060010",
      "country":"GB",
      "date":"20210712T143019+0200",
      "businessModelID":"0"
   },
   "consignments":[
      {
         "pickupPoint":"0",
         "consignmentID":"a000000123",
         "warehouseID":"W123",
         "warehouseRanking":"1",
         "shippingAddress":{
            "country":"GB",
            "postalCode":"SW1A 2AA",
            "town":"London"
         }
      }
   ]
}

Regards,
Pavan

Developer technologies | C#
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,546 Reputation points
    2021-07-23T18:30:28.807+00:00

    In your project open or create a C# file (cs file). Copy the sample JSON text to the clipboard. In the C# file use Edit | Paste Special then choose Paste JSON As Classes. You will get code as in the following.

    public class Rootobject  
    {  
        public Header header { get; set; }  
        public Consignment[] consignments { get; set; }  
    }  
    
    public class Header  
    {  
        public string orderID { get; set; }  
        public string country { get; set; }  
        public string date { get; set; }  
        public string businessModelID { get; set; }  
    }  
    
    public class Consignment  
    {  
        public string pickupPoint { get; set; }  
        public string consignmentID { get; set; }  
        public string warehouseID { get; set; }  
        public string warehouseRanking { get; set; }  
        public Shippingaddress shippingAddress { get; set; }  
    }  
    
    public class Shippingaddress  
    {  
        public string country { get; set; }  
        public string postalCode { get; set; }  
        public string town { get; set; }  
    }  
    

    Note that consignments is an array of Consignment objects. Then in Solution Explorer right-click the project and choose Manage NuGet Packages.... Install Newtonsoft.Json. Then add using Newtonsoft.Json; to the program. Then you can read the JSON using something as in the following.

    StreamReader sr = new StreamReader(infilename);  
    string json = sr.ReadToEnd();  
    Rootobject Root = JsonConvert.DeserializeObject<Rootobject>(json);  
    

    An alternative to Newtonsoft is System.Text.Json.Serialization that is part of .Net.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.