-1

I have a .Json URL that contains Json format data.

HTTPS://XXXX/fetch-transaction?fromdate=2020-10-13&toDate=2020-10-20 (Not working just an example)

[
    {
        "orderId": 110,
        "consignmentNumber": "TEST",
        "itemNumbers": [
            "TEST"
        ],
        "country": "UK",
        "orderType": "ORDER_HOME_DELIVERY",
        "paymentTransactionId": "395611",
        "priceInOre": 5900,
        "paidAt": "2020-10-16 10:51:08",
        "orderNumber": "7000067718",
        "articleName": "SOUTH-2"
    }
]

I would like to insert data into a SQL server table and wonder if it's possible to use SQL server and t-SQL directly here or should I go for VS and C#?

If C# is the preferred choice can someone pleae guide me on how I would accomplish it? I have created a Console application in Visual studio (however it might be a better solution to use something els then to create a command line applcation?) or guide me in the right direction.

1 Answer 1

1

You can try the following code to get the value from the json txt and transfer it to the sql server table.

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
class Program
    {
        static void Main(string[] args)
        {
            string json = File.ReadAllText("D:\\test1.txt");
            List<Example> list = JsonConvert.DeserializeObject<List<Example>>(json);
            string strcon = @"Connstr";
            SqlConnection connection = new SqlConnection(strcon);
            connection.Open();
            string sql = "Insert into JsonData(orderId,consignmentNumber,itemNumbers,country,orderType,paymentTransactionId,priceInOre,paidAt,orderNumber,articleName) values(@orderId,@consignmentNumber,@itemNumbers,@country,@orderType,@paymentTransactionId,@priceInOre,@paidAt,@orderNumber,@articleName)";
            SqlCommand command = new SqlCommand(sql, connection);
            foreach (Example item in list)
            {
                command.Parameters.AddWithValue("@orderId", item.orderId);
                command.Parameters.AddWithValue("@consignmentNumber", item.consignmentNumber);
                command.Parameters.AddWithValue("@itemNumbers", item.itemNumbers.First());
                command.Parameters.AddWithValue("@country", item.country);
                command.Parameters.AddWithValue("@orderType", item.orderType);
                command.Parameters.AddWithValue("@paidAt", item.paidAt);
                command.Parameters.AddWithValue("@paymentTransactionId", item.paymentTransactionId);
                command.Parameters.AddWithValue("@priceInOre", item.priceInOre);
                command.Parameters.AddWithValue("@articleName", item.articleName);
                command.Parameters.AddWithValue("@orderNumber", item.orderNumber);
            }
            command.ExecuteNonQuery();
            connection.Close();
      
            

        }
    }
    public class Example
    {
        public int orderId { get; set; }
        public string consignmentNumber { get; set; }
        public List<string> itemNumbers { get; set; }
        public string country { get; set; }
        public string orderType { get; set; }
        public string paymentTransactionId { get; set; }
        public int priceInOre { get; set; }
        public string paidAt { get; set; }
        public string orderNumber { get; set; }
        public string articleName { get; set; }
    }

Final result:

enter image description here

Edit:

var json = new WebClient().DownloadString("URL");
Sign up to request clarification or add additional context in comments.

3 Comments

One more thing, what namespace(Using) should i use here? or there is no need to use namespace(Using) with in console applications?
@Nils, you need to install nuget-package Newtonsoft.Json. Also, I have added the using statement in my answer.
should be 1st statement inside for loop. SqlCommand command = new SqlCommand(sql, connection); should be last statement of for loop. command.ExecuteNonQuery();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.