jsonConvert.SerializeObject converts Int32 to String

Hi there, I’ve got a datatable that I’ve imported from CSV. A column in the table is an amount (Int32). When I serialize the dt, the result has the amount converted to a string. I’m using: “jsonConvert.SerializeObject(outputFinal)” for the conversion.

Here’s an example:
{
“date”: “2019-08-30”,
“payee”: "SERVICE CHARGE ",
“memo”: “”,
“amount”: “-200”,
}

I’ve be super grateful for any advice on how I can prevent the value from being converted to a string?

1 Like

Hi @cnjackson

Do you want to read this JsonFile and convert it to object right ?

cheers :smiley:

Happy learning :smiley:

4 Likes

Hi @pattyricarte

The dt is already converted to a Json Object, the problem is the “amount” property is showing “-200” as a string - I need it to be an int ie, -200 (no “”)

The strange thing is the amount is an int in the data table so I don’t understand why it’s being converted to a string.

1 Like

@cnjackson

Convert.ToInt32(Jsonobject(“PropertyName”))

cheers :smiley:

Happy learning:D

4 Likes

Thanks - does that mean I need to iterate through the entire JsonObject and convert each value in the “amount” property one at a time, or is there a way to convert all the “amount” properties at once?

	{
		"date": "2019-08-30",
		"payee": "GOV STAMP DUTY ",
		"memo": "",
		"amount": "-100",
		"account_id": "f21b0dd44"
	},
	{
		"date": "2019-08-30",
		"payee": "COMPLIANCE FEE ",
		"memo": "",
		"amount": "-200",
		"account_id": "f21b0dd44"
	},
	{
		"date": "2019-08-30",
		"payee": "TO: CHARLES N JACKSON-Salary transfer",
		"memo": "",
		"amount": "-800000",
		"account_id": "f21b0dd44"
	},
1 Like

I think that was a json array so you need to iterate and convert it .cheers

2 Likes

I’m not sure I understand. The datatable has the amount set to Int32 already. The Serialize converts the amount to a String which is not what I want.

Should I first convert the datatable to a jsonArray and then serialize it to get all the ‘amount’ fields converted to Int32?

This is the result I’m after:

{
“date”: “2019-08-30”,
“payee”: "GOV STAMP DUTY ",
“memo”: “”,
“amount”: -100,
“account_id”: “f21b0dd44”
},
{
“date”: “2019-08-30”,
“payee”: "COMPLIANCE FEE ",
“memo”: “”,
“amount”: -200,
“account_id”: “f21b0dd44”
},
{
“date”: “2019-08-30”,
“payee”: “TO: CHARLES N JACKSON-Salary transfer”,
“memo”: “”,
“amount”: -800000,
“account_id”: “f21b0dd44”
}

1 Like

Hello @cnjackson
I tried your code jsonConvert.SerializeObject(outputFinal) and it is working fine.
Please make sure the datatype of column amount is integer and not string.

1 Like

@cnjackson No need to convert it to data table .Just pass the text file and serialize and it will return an object. Then do this Convert.ToInt32(Jsonobject(“PropertyName”))

Have you already try it ?

cheers :smiley:

Happy learning :smiley:

2 Likes