JSON encoding vs decoding
20 June 2023 (Updated 20 June 2023)
Encoding
You encode JSON when you convert a valid JSON object into a string. Here’s an example in Ruby:
require 'json'
person = { name: 'Bob', age: 30 }
JSON.generate(person)
# => "{\"name\":\"Bob\",\"age\":30}"
Decoding
You decode JSON when you convert a previously encoded JSON string into a JSON object. Here’s an example in Ruby:
json_string = "{\"name\":\"Bob\",\"age\":30}"
JSON.parse(json_string)
# => {"name"=>"Bob", "age"=>30}
Tagged:
Misc
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment