Feature #15541
closedAdd alias symbolize_keys for symbolize_names kwarg for JSON.parse
Description
https://github.com/ruby/psych/issues/341
When trying to symbolize keys on JSON parsing, it's really hard to remember the name symbolize_names
:
JSON.parse(data, symbolize_names: true)
I would like to propose that we change this keyword to symbolize_keys
to be more clear:
JSON.parse(data, symbolize_keys: true)
The documentation for this method also reflects the confusion: http://ruby-doc.org/stdlib-2.6/libdoc/json/rdoc/JSON.html#method-i-parse-21
symbolize_names: If set to true, returns symbols for the names (keys) in a JSON object. Otherwise strings are returned. Strings are the default.
The same issue came up in Psych not too long ago:
https://github.com/ruby/psych/issues/341
I believe the current name causes confusion. Would it be possible to add an alias to this keyword for clarity?
Updated by shevegen (Robert A. Heiler) almost 6 years ago
Personally I think that alias name would make sense. With a new alias the backwards
behaviour would also be retained.
Updated by baweaver (Brandon Weaver) almost 6 years ago
shevegen (Robert A. Heiler) wrote:
Personally I think that alias name would make sense. With a new alias the backwards
behaviour would also be retained.
An alias would probably be best as otherwise it would cause breaking changes, and I think we'd rather avoid that.
Updated by jsc (Justin Collins) almost 5 years ago
This would be a nice simple addition, as I have gotten this wrong several times. JSON.parse
does not raise any error when the wrong option name is given, so you only notice when trying to retrieve values with symbol keys returns nil
. After some debugging you might realize the keys are still strings.
Note that MultiJson uses symbolize_keys
: https://www.rubydoc.info/gems/multi_json/MultiJson#load-instance_method which may be where some confusion is coming from.
Updated by ioquatix (Samuel Williams) over 1 year ago
I was a strong proponent of this proposal, however I checked the JSON RFC https://www.rfc-editor.org/rfc/rfc8259 for guidance and found that it refers to them as name/value pairs. So symbolize_names
is not so strange for JSON.parse
.
An object is an unordered collection of zero or more name/value
pairs, where a name is a string and a value is a string, number,
boolean, null, object, or array.
I investigated several other instances in https://bugs.ruby-lang.org/issues/19607 if you are interested.
So the only conclusion I have at this time is:
-
symbolize_names
is unintuitive in general when the result is ultimately a Hash (table) instance with symbol keys. -
symbolize_names
aligns with the JSON RFC conventions (they are called name/value pairs). -
symbolize_keys
aligns with the YAML specification (they are called key/value pairs). - It's part of the public interface already and so at least non-trivial to change.
On this basis, I suggest we close this issue.
Updated by hsbt (Hiroshi SHIBATA) over 1 year ago
- Status changed from Open to Third Party's Issue
- Assignee set to hsbt (Hiroshi SHIBATA)