Search code examples
c#jsonhttpclienthttpresponse

How do I read a section in JSON result by httpClient?


Here's the C# code to retrieve the JSON:

try
        {
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.BaseAddress = new Uri(_serviceConfig.DataGapsBaseUrl);
            var request = new HttpRequestMessage(HttpMethod.Get, "/piper/jobs/" + jobId.ToString() + "/status");

            var token = await GetToken();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            var response = await client.SendAsync(request);
            var result = await response.Content.ReadAsStringAsync();
            var info = JObject.Parse(result);
            return true;
        }

How do extract this specific section (highlighted in yellow) in JSON returned? Dfid is the unique id for this record.

enter image description here

    {
    "outputs": {},
    "execution": [
        {
            "jobId": "8871d0ca0f324aae9685f768b00eddfb",
            "createTime": "2023-04-02T21:49:34.913+0000",
            "name": "dependency tasks",
            "startTime": "2023-04-02T21:49:34.934+0000",
            "id": "ae3d668f2bef4e5ca9ae262159b271ad",
            "label": "dependency tasks",
            "taskNumber": 1,
            "type": "dependency",
            "priority": 0,
            "tasks": [
                {
                    "dfId": "ad2bb0db-402a-47f6-928d-3fe4276ee417",
                    "name": "df_TCparamtest_alternateid_value_mapping_not_type_DOBAl_175234",
                    "type": "runDataFlow",
                    "label": "Data",
                    "userId": 17,
                    "livyServer": 101,
                    "parameters": "{\"limit_rows\":\"limit 2000\",\"Run_Id\":\"\",\"Batch_Id\":\"4229223\"}",
                    "stopOnTaskFailure": "N",
                    "stopDependentTasks": "N"
                }
            ],
            "status": "COMPLETED"
        },
        {
            "stopOnTaskFailure": "N",
            "label": "Data",
            "stopDependentTasks": "N",
            "type": "runDataFlow",
            "priority": 0,
            "userId": 17,
            "parentId": "ae3d668f2bef4e5ca9ae262159b271ad",
            "output": "{\"status\":\"Completed\",\"message\":[{\"name\":\"Transform_out\",\"status\":\"Completed\",\"message\":\"Transform_out executed successfully and processed 0 rows\"},{\"name\":\"Data_compare_unformattednumber\",\"status\":\"Completed\",\"message\":\"Data_compare_unformattednumber executed successfully. The Details are :  Duplicates In A : 0  Duplicates In B : 0  Only In A : 0  Only In B : 0  Difference : 0  Matched : 0 \"},{\"name\":\"startComponent\",\"status\":\"Completed\",\"message\":\"Component executed successfully\"},{\"name\":\"Transform_in\",\"status\":\"Completed\",\"message\":\"Transform_in executed successfully and processed 0 rows\"}],\"runId\":2863}",
            "executionTime": 87765,
            "jobId": "8871d0ca0f324aae9685f768b00eddfb",
            "livyServer": 101,
            "dfId": "ad2bb0db-402a-47f6-928d-3fe4276ee417",
            "createTime": "2023-04-02T21:49:34.924+0000",
            "name": "df_TCparamtest_alternateid_value_mapping_not_type_DOBAl_175234",
            "progress": 100,
            "startTime": "2023-04-02T21:49:34.927+0000",
            "id": "f38cf10e30a54c1694d32413a7d53689",
            "endTime": "2023-04-02T21:51:02.693+0000",
            "parameters": "{\"limit_rows\":\"limit 2000\",\"Run_Id\":\"\",\"Batch_Id\":\"4229223\"}",
            "status": "COMPLETED"
        }
 {
            "stopOnTaskFailure": "N",
            "label": "Data",
            "stopDependentTasks": "N",
            "type": "runDataFlow",
            "priority": 0,
            "userId": 11,
            "parentId": "47fd48802c2040849911dcef5f63aaa9",
            "output": "{status=Error, message=[GDP5007025 : Unable to run dataFlow in cluster/pipelineError details : GDP40407000: Unable to get or create the Livy sesssion]}",
            "executionTime": 10247,
            "jobId": "e6bef25e8aab48b59e550bf4b3317773",
            "livyServer": 101,
            "dfId": "346fc5a1-86f5-4642-aceb-49c3c161a206",
            "createTime": "2023-04-04T19:40:30.635+0000",
            "name": "df_TCparamtest_alternateid_value_mapping_DOBal_672375",
            "progress": 100,
            "startTime": "2023-04-04T19:40:30.639+0000",
            "id": "09801f3dc2f1448b9bdb6bcf109476f6",
            "endTime": "2023-04-04T19:40:40.886+0000",
            "parameters": "{\"limit_rows\":\"limit 1000\",\"Batch_Id\":\"null\"}",
            "status": "ERROR"
        }
    ],
    "inputs": {
        "Batch_Id": 4229223
    },
    "currentTask": -1,
    "label": "Pipeline",
    "priority": 0,
    "pipelineId": "5b51b5ba2af142c4b10a68db35f2dd18",
    "createTime": "2023-04-02T21:49:34.892+0000",
    "webhooks": [],
    "startTime": "2023-04-02T21:49:34.900+0000",
    "id": "8871d0ca0f324aae9685f768b00eddfb",
    "endTime": "2023-04-02T21:51:02.717+0000",
    "status": "COMPLETED"
}

Solution

  • To deserialize data in yellow, you have to fix "output" and "parameters" properies since they still remains a json string after parsing

       using Newtonsoft.Json;
    
        var jObj = JObject.Parse(result)["execution"][1];
        jObj["output"]=JObject.Parse( (string)jObj["output"]);
        jObj["parameters"]=JObject.Parse( (string)jObj["parameters"]);
    
        string dfid =  (string) jObj["dfId"];   // ad2bb0db-402a-47f6-928d-3fe4276ee417
        string outputStatus = (string) jObj["output"]["status"]; // Completed
    

    or you can deserialize it to a C# class instance

    
        Execution execution = jObj.ToObject<Execution>();
        outputStatus = execution.output.status; // Completed
    
    public class Execution
    {
        public string stopOnTaskFailure { get; set; }
        public string label { get; set; }
        public string stopDependentTasks { get; set; }
        public string type { get; set; }
        public int priority { get; set; }
        public int userId { get; set; }
        public string parentId { get; set; }
        public Output output { get; set; }
        public int executionTime { get; set; }
        public string jobId { get; set; }
        public int livyServer { get; set; }
        public string dfId { get; set; }
        public DateTime createTime { get; set; }
        public string name { get; set; }
        public int progress { get; set; }
        public DateTime startTime { get; set; }
        public string id { get; set; }
        public DateTime endTime { get; set; }
        public Parameters parameters { get; set; }
        public string status { get; set; }
    }
    
    public class Parameters
    {
        public string limit_rows { get; set; }
        public string Run_Id { get; set; }
        public string Batch_Id { get; set; }
    }
    
    public class Output
    {
        public string status { get; set; }
        public List<Message> message { get; set; }
        public int runId { get; set; }
    }
    
    public class Message
    {
        public string name { get; set; }
        public string status { get; set; }
        public string message { get; set; }
    }