Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sinatra won't parse the JSON automatically for you, but luckily parsing JSON is pretty straightforward:</p> <p>Start with requiring it as usual. <code>require 'rubygems'</code> if you're not on Ruby 1.9+:</p> <pre><code>&gt;&gt; require 'json' #=&gt; true &gt;&gt; a_hash = {'a' =&gt; 1, 'b' =&gt; [0, 1]} #=&gt; {"a"=&gt;1, "b"=&gt;[0, 1]} &gt;&gt; a_hash.to_json #=&gt; "{"a":1,"b":[0,1]}" &gt;&gt; JSON.parse(a_hash.to_json) #=&gt; {"a"=&gt;1, "b"=&gt;[0, 1]} </code></pre> <p>That's a roundtrip use to create, then parse some JSON. The IRB output shows the hash and embedded array were converted to JSON, then parsed back into the hash. You should be able to break that down for your nefarious needs.</p> <p>To get the fields we'll break down the example above a bit more and pretend that we've received JSON from the remote side of your connection. So, the <code>received_json</code> below is the incoming data stream. Pass it to the JSON parser and you'll get back a Ruby data hash. Access the hash as you would normally and you get the values:</p> <pre><code>&gt;&gt; received_json = a_hash.to_json #=&gt; "{"a":1,"b":[0,1]}" &gt;&gt; received_hash = JSON.parse(received_json) #=&gt; {"a"=&gt;1, "b"=&gt;[0, 1]} &gt;&gt; received_hash['a'] #=&gt; 1 &gt;&gt; received_hash['b'] #=&gt; [0, 1] </code></pre> <p>The incoming JSON is probably a parameter in your <code>params[]</code> hash but I am not sure what key it would be hiding under, so you'll need to figure that out. It might be called <code>'json'</code> or <code>'data'</code> but that's app specific.</p> <p>Your database code looks ok, and must be working if you're seeing some of the data written to it. It looks like you just need to retrieve the fields from the JSON.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload