question

babruvahana avatar image
0 Votes"
babruvahana asked babruvahana edited

Read Json Array in Azure functions using C#

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

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Are you sure this is an Azure question? It seems to be a C# question. Why are you using Azure when you can use plain C#?

2 Votes 2 ·

1 Answer

SimpleSamples avatar image
1 Vote"
SimpleSamples answered SimpleSamples edited

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.