# Have a problem JSON and Hash

**URL:** <https://www.ruby-forum.com/t/have-a-problem-json-and-hash/192436>\
**Category:** Rails\
**Created:** [July 30, 2010, 10:54am UTC](https://www.ruby-forum.com/t/have-a-problem-json-and-hash/192436 "2010-07-30T10:54:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vuonghung12a2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ce7236/32.png) [@vuonghung12a2](https://www.ruby-forum.com/u/vuonghung12a2)\
**Post date:** [July 30, 2010, 10:54am UTC](https://www.ruby-forum.com/t/have-a-problem-json-and-hash/192436/1 "2010-07-30T10:54:36Z")

</div>

Hi everybody.

I have using ActiveSupport::JSON library in rails

I have a json string:  
test= {“C1”:{“id”:“C1”,“mode”:“new”,“type”:“block”,“name”:“new block  
C1”,“unit”:"",“choices”:[],“index”:10},“R1”:{“id”:“R1”,“mode”:“new”,“type”:“text”,“name”:“new  
question  
R1”,“unit”:"",“choices”:[""],“index”:20},“R2”:{“id”:“R2”,“mode”:“new”,“type”:“text”,“name”:“new  
question  
R2”,“unit”:"",“choices”:[""],“index”:30},“R3”:{“id”:“R3”,“mode”:“new”,“type”:“text”,“name”:“new  
question  
R3”,“unit”:"",“choices”:[""],“index”:40},“C2”:{“id”:“C2”,“mode”:“new”,“type”:“block”,“name”:“new  
block  
C2”,“unit”:"",“choices”:[],“index”:50},“R4”:{“id”:“R4”,“mode”:“new”,“type”:“text”,“name”:“new  
question  
R4”,“unit”:"",“choices”:[""],“index”:60},“C3”:{“id”:“C3”,“mode”:“new”,“type”:“block”,“name”:“new  
block C3”,“unit”:"",“choices”:[],“index”:70}}

when i using:

temp = ActiveSupport::JSON  
result = temp.decode(test)

and I recieve: Result is not sort the same as param “test” i had before.

result =“R2choicesnamenew question  
R2modenewunittypetextidR2index30R3choicesnamenew question  
R3modenewunittypetextidR3index40R4choicesnamenew question  
R4modenewunittypetextidR4index60C1choicesnamenew block  
C1modenewunittypeblockidC1index10C2choicesnamenew block  
C2modenewunittypeblockidC2index50C3choicesnamenew block  
C3modenewunittypeblockidC3index70R1choicesnamenew question  
R1modenewunittypetextidR1index20”

I dont understand it.  
Please tell me a solution.  
Thanks for help.

---

<div class="post-metadata">

**Author:** ![vuonghung12a2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ce7236/32.png) [@vuonghung12a2](https://www.ruby-forum.com/u/vuonghung12a2)\
**Post date:** [July 30, 2010, 1:51pm UTC](https://www.ruby-forum.com/t/have-a-problem-json-and-hash/192436/2 "2010-07-30T13:51:40Z")

</div>

On Jul 30, 3:54 am, Vuong H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> R2",“unit”:“”,“choices”:[“”],“index”:30},“R3”:{“id”:“R3”,“mode”:“new”,“type “:“text”,“name”:“new  
> question  
> R3”,“unit”:””,“choices”:[“”],“index”:40},“C2”:{“id”:“C2”,“mode”:“new”,“type “:“block”,“name”:“new  
> block  
> C2”,“unit”:””,“choices”:[],“index”:50},“R4”:{“id”:“R4”,“mode”:“new”,“type”: “text”,“name”:“new  
> question  
> R4”,“unit”:“”,“choices”:[“”],“index”:60},“C3”:{“id”:“C3”,“mode”:“new”,“type “:“block”,“name”:“new  
> block C3”,“unit”:””,“choices”:[],“index”:70}}

That is not a string, but trying to assign a new hash to test. I’ll  
assume that the you just posted test= to define your problem.

> when i using:
> 
> temp = ActiveSupport::JSON  
> result = temp.decode(test)

The call to decode will create a hash From [ruby-doc.org](http://ruby-doc.org):

“A Hash is a collection of key-value pairs. It is similar to an Array,  
except that indexing is done via arbitrary keys of any object type,  
not an integer index. The order in which you traverse a hash by either  
key or value may seem arbitrary, and will generally not be in the  
insertion order.”

result = result.sort

\<%= result.inspect %\>

Seems to sort your hash by the keys. If input order is what you are  
looking for, not sure how to help without modifying your keys  
(prepending an index).

Steve A.
