Reputation: 111
have URl want to store json data to database automatically
site provide this code but use Java
HttpResponse response = Unirest.get("https://xproject.restdb.io/rest/data")
.header("x-apikey", "42ba8a6dc7532aa8319a58310281665e394e4")
.header("cache-control", "no-cache")
.asString();
This Example of json
model called speed contain speed
& id
need save gps_speed_kph = speed
& id_watch ==_id
Upvotes: 2
Views: 13949
Reputation: 464
You can use JSONB column. This may help you to update the single are the group of data based on the key in the column.
Ref:
1) http://edgeguides.rubyonrails.org/active_record_postgresql.html#json-and-jsonb
2) https://nandovieira.com/using-postgresql-and-jsonb-with-ruby-on-rails
Upvotes: 3
Reputation: 3339
Firstly, you have to specify the db column
data type as hash in
your model
as described here.
Then you have to save your json
as hash in your db (json to hash )
:
require 'json'
value = '{"val":"test","val1":"test1","val2":"test2"}'
myhash = JSON.parse(value)
Model.create(hash_column_name: myhash)
and for converting you can do this (hash to json)
:
@myhash = Model.find(1).hash_column_name
json_data = @myhash.to_json
Upvotes: 4