Skip to main content

Invalid json result

In rails some times we are getting invalid json outputs say for example, as per the json standard, both the attributes and its values should be enclosed with double quotes (") but in some of the ruby patches has issues with that. If you are facing such issues then this post will be useful:


Changes what we have to make in the file 'core.rb' which is located at:
LIB_PATH/ruby/gems/1.8/gems/activesupport-1.4.0/lib/active_support/json/encoders
in your rails server, the LIB PATH is /usr/local/lib
required function is "encoder"  line# 52
New Code
========
define_encoder Hash do |hash|
        returning result = '{' do
          result << hash.map do |key, value|
            key = ActiveSupport::JSON::Variable.new(key.to_s) if
              ActiveSupport::JSON.can_unquote_identifier?(key)
            "\"#{key.to_json}\": #{value.to_json}"                                            #=> This is line where we included the required quotes, thats it
          end * ', '
          result << '}'
        end
      end
Old Code
========
define_encoder Hash do |hash|
        returning result = '{' do
          result << hash.map do |key, value|
            key = ActiveSupport::JSON::Variable.new(key.to_s) if
              ActiveSupport::JSON.can_unquote_identifier?(key)
            "#{key.to_json}: #{value.to_json}"
          end * ', '
          result << '}'
        end
      end
Output Sample: [Json attribute key should be enclosed with quotes]
Old code will produce the json output as follows:
[
    {
        person: {
            id: 1,
            last_name: "test"
        }
    }
]
New code will produce the json output as follows [Note the json attribute keys are enclosed with quotes]
[
    {
        "person": {
            "id": 1,
            "last_name": "test"
        }
    }
]
You can validate the json code at http://jsonlint.com/ , also the site http://jsonviewer.stack.hu/ will helps you understand the hierarchy of the json output.

Comments

Popular posts from this blog

Errno::ENOENT: No such file or directory

HTML to PDF conversion is very easy in rails and pdfkit will be useful in this case, wkhtmltopdf tool will be supporting tool so you can easily convert an html to pdf.  In my experience I used the following steps: gem install pdfkit -v 0.5.0 gem install wkhtmltopdf -v 0.1.2 gem install wkhtmltopdf-binary Some times this sequence will not work, so I use the following as per the instruction from  https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF Be careful to download the appropriate wkhtmltopdf tar ball, it depends on your os architecture, like i386 or 64 wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2 tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2 mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf chmod +x /usr/local/bin/wkhtmltopdf Try to convert html to pdf after restarting your web servers.  If you get any error like: Errno::ENOENT: No such file or directory then I have a str...

Redis & nginx configuration for websocket/actionCable

Start redis server => redis-server redis console => redis-cli view available databases => config get databases pubsub channels global* pubsub channels global_notification_2 pubsub channels global_notification pubsub channels * => Lists all the available channels pubsub numsub global_notification_2 => returns the number of subscribers From unix command line directly redis-cli PUBSUB CHANNELS redis-cli PUBSUB NUMSUB global_notification_2 [root@api prod-openmednet-api]# redis-cli PUBSUB CHANNELS 1) "_action_cable_internal" 2) "global_notification_2" [root@api prod-openmednet-api]# redis-cli PUBSUB CHANNELS global_notification_2 1) "global_notification_2" [root@api prod-openmednet-api]# redis-cli PUBSUB NUMSUB global_notification_2 1) "global_notification_2" 2) (integer) 2 NGINX configuration:  worker_processes   auto; worker_rlimit_nofile 500000;  #=> refer RLIM...

Postgres points

Export sql data to file copy (select * from table-name) to '/tmp/cta.pl.list' in root dnf install postgresql-server postgresql-devel service postgresql start mkdir /home/pgdir chown postgres:postgres /home/pgdir -R chmod 777 /home/pgdir -R  su postgres initdb -D /home/pgdir pg_ctl -D /home/pgdir -l /home/pgdir/log start