1

I have a form to add json url form, If user add json url and submit form, I want to save all json data to db.[form]1

Fetching json data from url using httparty gem works perfectly, now to store json data to db json_url

json data
 {
        "restaurant_name": "Restaurant 3",
        "address": "xyz address",
        "country": "United States",
        "currency": "USD",
        "client_key": "12345",
        "client_name": "Client 3",
        "client_email": "test3@mail.com",
        "client_phone": "9876",
        "product_tier": "tier1",
        "brand_logo_large": {
            "ID": 37,
            "id": 37,
            "title": "bait-al-bahar-logo-design",
            "filename": "bait-al-bahar-logo-design.png",
            "filesize": 105071,
            "url": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
            "link": "http://codekyt.in/froodle-wp/projects/res-1-client-1/bait-al-bahar-logo-design/",
            "alt": "",
            "author": "1",
            "description": "",
            "caption": "",
            "name": "bait-al-bahar-logo-design",
            "status": "inherit",
            "uploaded_to": 35,
            "date": "2019-01-04 11:11:48",
            "modified": "2019-01-04 11:13:01",
            "menu_order": 0,
            "mime_type": "image/png",
            "type": "image",
            "subtype": "png",
            "icon": "http://codekyt.in/froodle-wp/wp-includes/images/media/default.png",
            "width": 600,
            "height": 500,
            "sizes": {
                "thumbnail": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design-150x150.png",
                "thumbnail-width": 150,
                "thumbnail-height": 150,
                "medium": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design-300x250.png",
                "medium-width": 300,
                "medium-height": 250,
                "medium_large": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
                "medium_large-width": 600,
                "medium_large-height": 500,
                "large": "http://codekyt.in/froodle-wp/wp-content/uploads/2019/01/bait-al-bahar-logo-design.png",
                "large-width": 600,
                "large-height": 500
            }
        }

}

I created a form to user & added json url,

<%= form_tag("/settings/json_url", method: "post") do  %>
  <%= label_tag :json_url %>
  <%= text_field_tag :json_url %>
  <%= submit_tag 'submit' %>
<% end %>

In db,

  t.string :key
  t.string :value 
  t.string :json_url

In controller,

def json_url

 # I am getting confused here 

end
ray
  • 5,454
  • 1
  • 18
  • 40
adarsh
  • 306
  • 5
  • 16
  • You have to create another table for storing json data with key values. And one question is your json data structure is fixed or dynamic ? – Tanay Sharma Jan 22 '19 at 08:18
  • 1
    Hi Raj, welcome to StackOverflow. Your question is probably too broad, since it can be broken into multiple problems. You are asking about saving JSON in SQLite, which is a question on its own: [How can I store JSON in SQLite](https://stackoverflow.com/questions/16603621/how-to-store-json-object-in-sqlite-database). The second part fo your question is about how to fetch the data, and store it in the database. You've asked a similar question [here](https://stackoverflow.com/questions/54229537/parse-json-from-httparty-using-ruby-on-rails) already, so it's hard to know where you get stuck. – jdno Jan 22 '19 at 08:31
  • First fetch data from json url. `data = JSON.load(open(source)) #source is your url ` – Tanay Sharma Jan 22 '19 at 08:32
  • @t s it fixed json data – adarsh Jan 22 '19 at 08:35
  • @ts Fetching json data from url using httparty gem work perfect,now i want to store that json data (https://stackoverflow.com/questions/54229537/parse-json-from-httparty-using-ruby-on-rails) – adarsh Jan 22 '19 at 08:36
  • create a new field as string type and save whole data into this field or iterate over `data` object and then save accordingly. – Tanay Sharma Jan 22 '19 at 09:17
  • error = no implicit conversion of String into Integer... sett.each do |data| data.create(restaurant_name: data["restaurant_name"] ) sett.save end – adarsh Jan 24 '19 at 07:48

0 Answers0