Resolved SSIS Send JSON Data HTTPClient

Beanymattd

Member
Joined
Oct 12, 2023
Messages
6
Programming Experience
Beginner
Good morning,

I have a folder full of json format files which need to be sent via C# inside a script task inside SSIS to an API. The SSIS will loop through (foreach it setup) and send each file.

Below is the format of all the files(all same format but different values etc):

Json:
 "Time":"2018-10-21T00:08:03",

    "Control_Code": "JKX",

    "metrics": [

        {

            "Metric": 3721,

            "Organisation":"AD450",

            "Value": 20,

            "Datetime":"2018-10-21T00:08:00"

        },

        {

            "Metric": 1234,

            "Organisation":"HG650",

            "value": 88,

            "datetime":"2018-10-21T00:08:00"

        }

      

    ]

}

Can anybody assist or point me in the right direction on how to do this please?

I've been digging around online and found the following example starting point. Not sure if this can be adapted accordingly? Any help is appreciated.

C#:
using (var client = new HttpClient())
{
  client.BaseAddress = new Uri("baseurl");

  var data = JObject.FromObject(new
  {
    name = "some value",
    version = new {type = "some value", ver = 2}
  });

  var response = client.PostAsync("/urlpath",
                                  new StringContent(JsonConvert.SerializeObject(data),
                                  Encoding.UTF8, "application/json")).Result;

  response.EnsureSuccessStatusCode();

  var responseContent = await response.Content.ReadAsStringAsync();

  dynamic json = JsonConvert.DeserializeObject(responseContent);
  dynamic r = json.result;

  result = r.someproperty.ToString().Equals("resultdata");

}
 
That is a bad sample to start with. HttpClient. should not be disposed. You should only make one instance and keep it around for the lifetime of the program.

Next, without knowing what the API is and what format the data it expects it is hard for us to tell how good or bad that code is as a starting point.

Also a few posts ago, you started off asking how to create the files that are now sitting in that folder. It seems to me that you don't even need those files if you are just going to turn around and send the data in the files to the API. Seems like a wasted step.
 

Latest posts

Back
Top Bottom