简体   繁体   中英

My C# JavaScriptSerializer output is empty?

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;
    }

edits

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.

https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-server-2017

Use newtonsoft available via nuget.

Example:

Product product = new Product();

product.Name = "Apple";
product.Sizes = new string[] { "Small", "Medium", "Large" };

string output = Newtonsoft.Json.JsonConvert.SerializeObject(product);

{
    "Name": "Apple",
    "Sizes": [ "Small","Medium","Large"]
}

Maybe try this pseudo code:

//Just make sure that Dts.Variables["User::JSON"] actually returns a valid object.       
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;

FINALLY!

variable JSON must be set to object The query result should be set to Full result set

Before running a script task I had to run it through a foreach loop using 'ForEach ADO Enumerator' on the first table Under Variable Mapping assign this out to a new variable of type object.

Now in my script task all I need to do is:

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

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

Not sure why i have to read out the object and assign it to another variable in the foreach container but that is the key to my troubles.

This lead me to my solution: Assigning value from single row result set in ssis giving error in SSIS 2012

http://www.rad.pasfu.com/index.php?/archives/18-Foreach-Loop-based-on-Variable-SSIS.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM