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 6559949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:23:50+00:00 2026-05-25T13:23:50+00:00

Hey guys so I’m trying to construct some json based on a form submission.

  • 0

Hey guys so I’m trying to construct some json based on a form submission. My json needs to be something like this (truncated for simplicity):

{"id":0,"creditCard":{"address":{"state":"AZ"}}}

Basically I’m trying to do it like this:

json_hash = Hash.new
json_hash["id"] = 0
json_hash["creditCard"]["address"]["state"] = "test"
json_hash.to_json

But its throwing this errror:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

At the 3rd line in the example given (the deeply nested thing). Do ruby hashes work like this? Or are there extra steps I need to take?

  • 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-05-25T13:23:50+00:00Added an answer on May 25, 2026 at 1:23 pm

    Because at the third line you’re evaluating a json_hash['creditCard'] which is nil, then it has no method [].

    You can build an hash like that using

    json_hash = { "id" => 0,"creditCard" => { "address" => {"state" => "AZ"}}}
    

    and it has the given format.

    Thats a short syntax, to specify the fact that what you want in json_hash it’s NOT a single hash. It is an hash which has the value 0 in its "id" key, then in its "creditCard" it stores another hash which contains another hash (i.e. {"state" => "AZ"}) in its "address" key.

    Look at this pry transcript:

    pry(main)> json_hash = { "id" => 0,"creditCard" => { "address" => {"state" => "AZ"}}}
    => {"id"=>0, "creditCard"=>{"address"=>{"state"=>"AZ"}}}
    pry(main)> json_hash.object_id
    => 2175368520
    pry(main)> json_hash['creditCard'].object_id
    => 2175368560
    pry(main)> json_hash['creditCard']['address'].object_id
    => 2175368600
    

    all these values have different object_id so they are not the same thing, neither property of the same thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, trying to optimize this query to solve a duplicate user issue: SELECT
hey guys I have the follow code in js: $(document).ready(function(){ $(.replyLink).click(function(){ $(#form-to-+this.id).html(htmlForm()).toggle(500); return false;
Hey guys, I have a problem (again). This time I am trying to use
Hey guys, I want to store a categorized list of URLs. This is an
Hey Guys, here is my code for this, the only help i get from
Hey guys i am trying to automate my system at work for sending artwork
Hey guys, working on an event calendar. I'm having some trouble getting my column
Hey guys, I am trying to figure out how to disable the ability for
Hey guys i'm making a website but for some reason my div tags are
Hey guys I'm trying to add the string 'url( ) ' around my a

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.