1

I'm trying to create a timeline, with a form for a user to input a new event. I've decided to use the timeline from http://timeline.verite.co/ - I can create a form for each user to input a new event, and save that event to the database no problem. However, what I'm having trouble figuring out is:

a) Getting the data from the database and turning it into the correct JSON format that the timeline would be happy with - I have no idea where to start with this! Do I need to name the columns in the database with the same name as the JSON attributes, e.g. "caption"? And how do I then turn this into JSON?

b) How do I generate the JSON doc on the fly? The code requires a link to a JSON doc with the data.

This is the only way I can figure out how to do this - as if I was to generate a JSON doc for each user and insert each new event, then read from this, then surely there is some security issues with a JSON doc being easier to download by someone who doesn't own it, than data in a database?

Any help or pointers would be much appreciated - I'm pretty new to Rails and haven't had to really touch JSON yet! Thank you.

In case it helps, the data format required by the timeline is this:

{
  "timeline":
  {
      "headline":"The Main Timeline Headline Goes here",
      "type":"default",
      "text":"<p>Intro body text goes here, some HTML is ok</p>",
      "asset": {
          "media":"http://yourdomain_or_socialmedialink_goes_here.jpg",
          "credit":"Credit Name Goes Here",
          "caption":"Caption text goes here"
      },
      "date": [
          {
              "startDate":"2011,12,10",
              "endDate":"2011,12,11",
              "headline":"Headline Goes Here",
              "text":"<p>Body text goes here, some HTML is OK</p>",
              "tag":"This is Optional",
              "asset": {
                  "media":"http://twitter.com/ArjunaSoriano/status/164181156147900416",
                  "thumbnail":"optional-32x32px.jpg",
                  "credit":"Credit Name Goes Here",
                  "caption":"Caption text goes here"
              }
          }
      ],
      "era": [
          {
              "startDate":"2011,12,10",
              "endDate":"2011,12,11",
              "headline":"Headline Goes Here",
              "text":"<p>Body text goes here, some HTML is OK</p>",
              "tag":"This is Optional"
          } 

        ]
    }
}
3
  • You can easily turn Ruby Hash to JSON with data_hash.to_json. How to put data from the DB into hash is another question, it depends much on your DB structure and your knowledge of Ruby and ActiveRecord. Commented Oct 11, 2012 at 11:53
  • Ok, could I pull the data out of the database as a Ruby object and then convert it to JSON? How would I do this and then get it into the correct format? As you might be able to tell, my knowledge on the inner workings of the db is fairly limited - I've only created applications that create and read data, never tampering with it in this way. Any help would be great, but I'm afraid you might have to spell it out a bit for me! Thanks. Commented Oct 11, 2012 at 11:58
  • I agree with saverio's answer, for JSON structure as complex as you need it is much better to use JSON templates rather than to_json. Commented Oct 11, 2012 at 14:51

3 Answers 3

2

Using #to_json is not really an option, too complex.

Try to use JSON templates with JBuilder (see https://github.com/rails/jbuilder).

You can even use partials and cache, like a normal ERB view.

Sign up to request clarification or add additional context in comments.

2 Comments

That looks like just what I am after, thanks for that. However, how would I then pass the resulting JSON into the timeline, as it requests a path to the JSON to use as the source data? Thanks.
The wiew will be served by your server with a route, something like http://www.yourserver.com/timelines/42.json should output the JSON corresponding to the timeline #42
0

If you convert your json object into a string and stored in the database as blob type, then while retrieving you can use

ActiveSupport::JSON.decode(VALUE)

to convert the string to json

Comments

0

Try using Rabl. It will let you export simple and complex json structures with views in your rails application. It's extremely easy to set up.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.