Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8799323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:17:39+00:00 2026-06-14T00:17:39+00:00

Anyone else having this issue? hash_as_key = {‘one’ => 1, ‘two’ => 2} outer_hash

  • 0

Anyone else having this issue?

hash_as_key = {'one' => 1, 'two' => 2}
outer_hash = {hash_as_key => 3} 

outer_hash.keys[0].class
=> Hash

as_json = outer_hash.to_json
back_to_obj = JSON.parse(as_json)

back_to_obj.keys[0]
=> "{\"one\"=>1, \"two\"=>2}"
back_to_obj.keys[0].class
=> String

If you have a Hash object where a key is a hash itself, converting to JSON and reverting back to a Ruby object seems to turn the key into a String.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:17:40+00:00Added an answer on June 14, 2026 at 12:17 am

    The keys in JSON objects are strings and nothing else:

    An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

    enter image description here

    So your keys will become strings as soon as your data structure is converted to JSON. Then, once your data is in JSON, the original type information for your keys will be lost.

    Ruby’s Hashes are a fair bit more flexible and general than JSON’s objects so not every Ruby Hash can be represented in JSON. If you’re targeting JSON for data storage and transfer, then you’ll have to limit your data to things that JSON supports or translate your data into a structure that is JSON-compatible.

    If you look at the intermediate JSON you’ll see what’s going on:

    > puts outer_hash.to_json
    {"{\"one\"=>1, \"two\"=>2}":3}
    

    So the key is the string '{"one"=>1, "two"=>2}' because that’s what hash_as_key.to_s gives you. Similarly, a numeric key will be stringified with to_s and lose its “numberness”:

    > {1 => 2}.to_json
    => "{\"1\":2}" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I searched and couldn't find anyone else having this problem: When I try
Has anyone else had this issue and found a working solution? I've enabled the
I am curious to see if anyone else has run into this same issue...
I am having an issue with a WSDL that no one else around me
I am not sure if anyone else is having this problem or not with
Is anyone else having problems with InfoBox ? Is there a better way to
Has anyone else come across this at all? I used to be able to
has anyone else experienced this . I have SP1 but i have to kill
Has anyone else experienced this? I have uninstalled and reinstalled TortoiseSVN as well as
I wonder if anyone else is facing the same issue. I have a UITextView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.