user4912134 user4912134 - 23 days ago 8
JSON Question

Omit decimal places in the selected record

We are having a Web API which selects records from the oracle database and return result in JSON format. There are few fields in the table which is of Number data type. So in the JSON response the fields that are of Number data type are having decimal places like 123.0. Is there a way we can get just integer value and omit the decimal places like 123

public class DataController : ApiController
{
[HttpGet]
public HttpResponseMessage Getdetails(string id, DateTime date_in)
{
var prms = new List<OracleParameter>();
prms.Add(new OracleParameter("id", OracleDbType.Varchar2, id, ParameterDirection.Input));
prms.Add(new OracleParameter("date_in", OracleDbType.Date, date_in, ParameterDirection.Input));
var connStr = ConfigurationManager.ConnectionStrings["DtConnection"].ConnectionString;
using (OracleConnection dbconn = new OracleConnection(connStr))
{
DataSet userDataset = new DataSet();
var strQuery = "SELECT REQUEST_ID from SAMPLE where id = :id and date_in = :date_in ";
var returnObject = new { data = new OracleDataTableJsonResponse(connStr, strQuery, prms.ToArray()) };
var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));
ContentDispositionHeaderValue contentDisposition = null;
if (ContentDispositionHeaderValue.TryParse("inline; filename=TGSData.json", out contentDisposition))
{
response.Content.Headers.ContentDisposition = contentDisposition;
}
return response;
}
}
}


We have two class for handling the JSON

[JsonConverter(typeof(OracleDataTableJsonResponseConverter))]
public sealed class OracleDataTableJsonResponse
{
public string ConnectionString { get; private set; }
public string QueryString { get; private set; }
public OracleParameter[] Parameters { get; private set; }
public OracleDataTableJsonResponse(string connStr, string strQuery, OracleParameter[] prms)
{
this.ConnectionString = connStr;
this.QueryString = strQuery;
this.Parameters = prms;
   }   }


public class OracleDataTableJsonResponseConverter : JsonConverter
  {
public override bool CanConvert(Type objectType)
{
return objectType == typeof(OracleDataTableJsonResponse);
   }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException("OracleDataTableJsonResponse is only for writing JSON.  To read, deserialize into a DataTable");
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var response = (OracleDataTableJsonResponse)value;
using (var dbconn = new OracleConnection(response.ConnectionString))
{
    dbconn.Open();
using (var selectCommand = new OracleCommand(response.QueryString, dbconn))
{
if (response.Parameters != null)
selectCommand.Parameters.AddRange(response.Parameters);
using (var reader = selectCommand.ExecuteReader())
{
writer.WriteDataTable(reader, serializer);
} } }}}
public static class JsonExtensions
  {
public static void WriteDataTable(this JsonWriter writer, IDataReader reader, JsonSerializer serializer)
{
if (writer == null || reader == null || serializer == null)
throw new ArgumentNullException();
writer.WriteStartArray();
while (reader.Read())
{
  writer.WriteStartObject();
for (int i = 0; i < reader.FieldCount; i++)
{
writer.WritePropertyName(reader.GetName(i));
serializer.Serialize(writer, reader[i]);
   }
writer.WriteEndObject();
}
writer.WriteEndArray();


JSON result looks like

{"data":[{"REQUEST_ID":333838.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":362692.0},
{"REQUEST_ID":359544.0}]}


In the Oracle DB the field looks like

enter image description here

is there any possibility to select the fields which are of Number datatype omit the decimal places.

Answer

you need to update below code block in your code where you serialize values

for (int i = 0; i < reader.FieldCount; i++)
{
    writer.WritePropertyName(reader.GetName(i));
    serializer.Serialize(writer, reader[i]);
}

change as below,

for (int i = 0; i < reader.FieldCount; i++)
{
    string name =reader.GetName(i);
    writer.WritePropertyName(name);
    if(name  == "REQUEST_ID")
    {
       serializer.Serialize(writer, string.Format("{0:n0}",reader[i]));
    }else
    {
      serializer.Serialize(writer, reader[i]);
    }
}