Athul Santhosh
Athul Santhosh

Reputation: 246

how to convert a JSON object to corresponding string

How can i convert the below JSON Object to a string that can be converted to string and stored in string field and then use it later to be converted back to JSON

The JSON is:

{"tds_head_id"=>88, "date"=>nil, "salary_gross_amount"=>0, "qualifying_amount"=>0, "proof_amount"=>0, "remarks"=>nil}

this hash has to be converted such that it can be converted back to the same JSON format later.

Upvotes: 5

Views: 12089

Answers (1)

GPks
GPks

Reputation: 136

You can make use of the JSON library generate method to do so and get the corresponding string like

json_object = {"tds_head_id"=>88, "date"=>nil, "salary_gross_amount"=>0, "qualifying_amount"=>0, "proof_amount"=>0, "remarks"=>nil}
JSON.generate(json_object)

output will be

  "{\"tds_head_id\":88,\"date\":null,\"salary_gross_amount\":0,\"qualifying_amount\":0,\"proof_amount\":0,\"remarks\":null}"

you can convert it back to JSON by using JSON.parse Method

Upvotes: 9

Related Questions