Search code examples
jsonwebapilarge-data-volumes

Web API returning null JSON objects C#


I have a web API returning 117k JSON objects.

Edit: The API is calling MySQL to fetch 117k rows of data, putting them into a IEnumerable and sending them through JSON

All I see is [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},... the entire page...

I wanted to ask how someone what is happening and how you would handle a large JSON transfer. Prefer to get it all in one go to avoid querying back and forth (delay time).

The function call is this:

public IEnumerable<Score> Get(int id)
    {
    string mConnectionString = System.Configuration.ConfigurationManager.AppSettings["mysqlConnectionString"];
    MySqlConnection mConn;
    MySqlDataReader mReader;
        List<Score> returnedRows = new List<Score>();
        if (String.IsNullOrEmpty(mConnectionString))
        {
            return returnedRows;
        }
        try
        {
            // prepare the dump query
            MySqlCommand dumpCmd;
            string query = "SELECT * FROM score where id = "+id+";";

            using (mConn = new MySqlConnection(mConnectionString))
            {
                using (dumpCmd = new MySqlCommand())
                {
                    dumpCmd.Connection = mConn;
                    dumpCmd.CommandText = query;
                    mConn.Open();

                    mReader = dumpCmd.ExecuteReader(); /
                    if (mReader.HasRows)
                    {
                        while (mReader.Read())
                        {
                            string[] rowCols = new string[mReader.FieldCount]; // there are 20+ columns, at least the primary keys are not null
                            for (int i = 0; i < rowCols.Length; ++i)
                            {
                                rowCols[i] = mReader.GetString(i);
                            }
                            returnedRows.Add(new Score(rowCols));
                        }
                        mConn.Close();
                        return returnedRows;
                    }
                    else
                    {
                        // should return a 404 cause nothing found
                        mConn.Close();
                    }
                }
            }
        }
        catch (Exception e)
        {
            return returnedRows;
        }
        return returnedRows;
    }

Solution

  • Either mReader.GetString(i) is returning null or you have no data in the columns.