Quantcast
Channel: SQL Server Integration Services forum
Viewing all articles
Browse latest Browse all 24688

SSIS Script Task on Query Result using FOR JSON (ERROR)

$
0
0

I'm pulling a result set from SQL server into a C# object, where I then use the JavaScriptSerializer to convert it to a json string and output it to a file. The file is created but it only contains

{}

I'm not sure what's going on. I know the SQL query returns data in SSMS as I expect (just that it's too large to display the full output in SSMS). In SSIS where I'm doing the querying and script tasks it seems the result set is empty.....


Here is my Script task:

public void Main()
    {
        // TODO: Add your code here
        JavaScriptSerializer js = new JavaScriptSerializer();
        var myJSON = Dts.Variables["User::JSON"];
        string json = js.Serialize(myJSON);

        //string myJSON  = (Dts.Variables["User::JSON"].Value).ToString();

        using (System.IO.StreamWriter file =
        new System.IO.StreamWriter(@"C:\Users\myUser\Documents\TEST.JSON", true))


        {
            file.WriteLine(json);
        }

        Dts.TaskResult = (int)ScriptResults.Success;
    }

To add more context my query result that I'm reading into my Script task is such:

Select x,y,z from [table] wher x = 123
FOR JSON AUTO

This does return a json formatted text string in SSMS. IN SSIS I've set teh query to return 'Full Result Set' and store it in an object variable called 'JSON'.

I've tried setting this variable to type string, which returns an error that cannot convert object type to string.

I've also tried using newtonsoft.json with no luck. I get a generic script task error.

string output = Newtonsoft.Json.JsonConvert.SerializeObject(Dts.Variables["User::JSON"]);

    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\myUser\Documents\TEST.JSON", true))
    {
        file.WriteLine(json);
    }

    Dts.TaskResult = (int)ScriptResults.Success;



Viewing all articles
Browse latest Browse all 24688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>