1

I have a command line application. the way to call it is :

ruby comb.rb --format json 'journals.csv' 'articles.csv' 'authors.json' > full_articles.json

Basically what it does is combining information from different files (the ones you pass as arguments). The format defines the format of the output.

An array of arrays arribes to the json presenter class (collection in the snipped).

I want to insert to a new file(the one I pass as the last argument) a json object (an array of objects).

This is part of the presenter class:

def call
      collection.each do |line|
        puts as_json line
      end
    end

    private

    def as_json line
      {
        "issn"    => line[0],
        "title"   => line[1],
        "doi"     => line[2],
        "author"  => line[3],
        "journal" => line[4]
      }
    end

The input I get to the full_articles.jsonfile is a set of objects but not encapsulated inside an array. Do you know how to solve this problem?

4
  • Could you clarify what you mean by "a set of objects but not encapsulated inside an array." ? Commented Nov 3, 2015 at 15:46
  • @rohit89 the file looks like: {...} \n {...} \n {...} Commented Nov 3, 2015 at 16:08
  • And you want it to be as [{...}, {...}, {...}] ? Commented Nov 3, 2015 at 16:16
  • @rohit89 yes, this way. Commented Nov 3, 2015 at 16:32

2 Answers 2

1

If you want it as an array, you can use map

puts collection.map {|line| as_json line}.inspect

or

puts collection.map {|line| as_json line}.to_json
Sign up to request clarification or add additional context in comments.

3 Comments

Same output as before. @rohit89
Wait, is this supposed to be in json?
It has to be in a Json format, but not stringified. @rohit89
0

The solution is:

puts collection.map {|line| as_json line}.inspect

The way @rohit89 it was right but the array was not shown. Whit inspect it is shown.

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.