{"id":15602412,"url":"https://github.com/melashu/json_or_ruby_to_csv","last_synced_at":"2025-04-24T07:25:21.486Z","repository":{"id":168174385,"uuid":"643515091","full_name":"melashu/json_or_ruby_to_csv","owner":"melashu","description":"This gem converts arrays of JSON objects, arrays of hashes, arrays of ActionController params, simple hashes, or ActiveRecord relations and objects to a CSV-formatted string.","archived":false,"fork":false,"pushed_at":"2023-06-08T19:53:21.000Z","size":14,"stargazers_count":5,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-30T08:07:46.156Z","etag":null,"topics":["ruby","ruby-gem"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/melashu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-21T12:13:16.000Z","updated_at":"2023-05-30T03:05:51.000Z","dependencies_parsed_at":"2023-06-28T19:30:48.002Z","dependency_job_id":null,"html_url":"https://github.com/melashu/json_or_ruby_to_csv","commit_stats":null,"previous_names":["melashu/json_or_ruby_to_csv"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melashu%2Fjson_or_ruby_to_csv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melashu%2Fjson_or_ruby_to_csv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melashu%2Fjson_or_ruby_to_csv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/melashu%2Fjson_or_ruby_to_csv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/melashu","download_url":"https://codeload.github.com/melashu/json_or_ruby_to_csv/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250581465,"owners_count":21453675,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ruby","ruby-gem"],"created_at":"2024-10-03T02:41:26.157Z","updated_at":"2025-04-24T07:25:21.480Z","avatar_url":"https://github.com/melashu.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003ca name=\"readme-top\"\u003e\u003c/a\u003e\n\n# 📗 Table of Contents\n[![json_or_ruby_to_ - csv](https://img.shields.io/badge/json__or__ruby__to__-csv-2ea44f)](https://rubygems.org/gems/json_or_ruby_to_csv)\n\n- [📗 Table of Contents](#-table-of-contents)\n- [📖 json\\_or\\_ruby\\_to\\_csv ](#-json_or_ruby_to_csv-)\n  - [Installation ](#installation-)\n    - [Usage](#usage)\n      - [Using array\\_or\\_hash\\_to\\_csv](#using-array_or_hash_to_csv)\n      - [Using activerecord\\_to\\_csv](#using-activerecord_to_csv)\n      - [json\\_or\\_ruby\\_to\\_csv usage with rails controller](#json_or_ruby_to_csv-usage-with-rails-controller)\n  - [👥 Authors ](#-authors-)\n  - [🤝 Contributing ](#-contributing-)\n  - [⭐️ Show your support ](#️-show-your-support-)\n\n# 📖 json_or_ruby_to_csv \u003ca name=\"about-project\"\u003e\u003c/a\u003e\n\nThis gem converts data from a variety of sources into a comma-separated value (CSV) format. The gem can convert arrays of JSON objects, arrays of hashes, arrays of ActionController params, simple hashes, or ActiveRecord relations and objects. The CSV format is a common way to store data in a tabular format. It is often used for data exchange between different applications.\n\n## Installation \u003ca name=\"tech-stack\"\u003e\u003c/a\u003e\n\nAdd the following code to you Gemfile \n\n```\ngem 'json_or_ruby_to_csv'\n```\nor \n\ninstall the gem on your terminal \n\n```\ngem install json_or_ruby_to_csv\n```\n    \n\n### Usage\n\n`require` this gem on the top of your ruby code.\n\n#### Using array_or_hash_to_csv\n\n\nIf you want to convert `arrays of JSON objects`, `arrays of hashes`, `arrays of ActionController params`, and `simple hashes` to csv you can use `array_or_hash_to_csv` method from `JsonOrRubyToCsv`. Let's see how:-\n\n\u003e Don't forget to include `JsonOrRubyToCsv` module in your ruby class.\n\n\u003ci\u003eFor example \u003c/i\u003e\n\n```ruby\n\nrequire 'json_or_ruby_to_csv'\nclass Employee\n  include JsonOrRubyToCsv\n\n  def get_csv_data(data_list)\n   array_or_hash_to_csv(data_list)\n  end\nend\n```\nLet's create `Employee` object and send array of object to `get_csv_data` method.\n  \n```ruby\nemployee = Employee.new\ndata_list =  [\n    { \"id\": 200,\n      \"fname\": \"Meshu\",\n      \"lname\": \"Amare\",\n      \"salary\": 4500,\n      \"company\": \"XYZ.com\",\n      \"position\": \"DevOps IT Operations\"\n    },\n    \n    { \"id\": 201,\n      \"fname\": \"Solomon\",\n      \"lname\": \"Alebachew\",\n      \"salary\": 5000,\n      \"company\": \"XYZ.com\",\n      \"position\": \"Rails Developer\"\n    },\n    {\n      \"id\": 202,\n      \"fname\": \"Bini\",\n      \"lname\": \"Ew\",\n      \"salary\": 5000,\n      \"company\": \"XYZ.com\",\n      \"position\": \"Senior Sofware Developer\"\n    }\n  ]\n\nprint employee.get_csv_data(data_list)\n\n```\n\n**The output looks like below**\n\n```csv \nid,fname,lname,company,position,salary\n200,Meshu,Amare,XYZ.com,DevOps IT Operations,4500\n201,Solomon,Alebachew,XYZ.com,Rails Developer,5000\n203,Bini,Ew,XYZ.com,Senior Sofware Developer,5000 \n```\n\n#### Using activerecord_to_csv\n\nIf you want to conver `ActiveRecord::Relation` **(collection of objects fetched by `.all` or `.where()`)** or simple ActiveRecord object to csv formated-string you can use `activerecord_to_csv` method from `JsonOrRubyToCsv` module.\n\n*For example*\n\n```ruby\n\nrequire 'json_or_ruby_to_csv'\nclass Employee\n  include JsonOrRubyToCsv\n\n  def get_data_as_csv\n    all_employee = Employee.select(:id, :fname, :lname, :salary, :company, :position)\n     activerecord_to_csv(all_employee)\n  end\n \nend\n\n```\nHere `get_data_as_csv` method first retrive data from the database, then pass to `activerecord_to_csv` method and return the data as comma-separated csv formated string. \n\nLet's create `Employee` object and call `get_data_as_csv` method.\n\n```ruby\nemployee = Employee.new\nemployee.get_data_as_csv\n\n```\n\n**The output looks like below**\n\n```csv \nid,fname,lname,company,position,salary\n200,Meshu,Amare,XYZ.com,DevOps IT Operations,4500\n201,Solomon,Alebachew,XYZ.com,Rails Developer,5000\n203,Bini,Ew,XYZ.com,Senior Sofware Developer,5000 \n```\n\n#### json_or_ruby_to_csv usage with rails controller  \n\nYou can include the functionality of this gem in your controller class.\n\n*for example*\n\nIf you want your controller action to return a CSV formated data after fetching data from the database, you can do it as follow.\n\n```ruby\nrequire 'json_or_ruby_to_csv'\nclass ConvertsController \u003c ApplicationController\n  include JsonOrRubyToCsv\n  def index\n    input = params_value[:data]\n    result = array_or_hash_to_csv(input)\n    render plain: result, status: :ok\n  end\n\n\n  def previous\n    all_employee = Employee.select(:id, :fname, :lname, :salary, :company, :position)\n    result = activerecord_to_csv(all_employee)\n    render plain: result, status: :ok\n  end\n\n  private\n\n  def params_value\n    params.require(:data) # there must be data\n    params.permit(data: %i[id fname lname company position salary]) # list of optional attributes\n  end\nend\n\n\n```\n\n`index` action controller method will convert array of params data to csv formated string where as `previous` will return csv formated string after fetching data from database.\n\n## 👥 Authors \u003ca name=\"authors\"\u003e\u003c/a\u003e\n\n 👤 Melashu Amare\n\n- GitHub: [@melashu](https://github.com/melashu)\n- Twitter: [@meshu102](https://twitter.com/meshu102)\n- LinkedIn: [Melashu Amare](https://www.linkedin.com/in/melashu-amare/)\n\n## 🤝 Contributing \u003ca name=\"contributing\"\u003e\u003c/a\u003e\n\nThis repo is open for contributions. Issues, and feature requests are welcome!\n\nFeel free to check the [issues page.](https://github.com/melashu/json_or_ruby_to_csv/issues)\n\n## ⭐️ Show your support \u003ca name=\"support\"\u003e\u003c/a\u003e\n\nGive a star if you like this project!\n  \n\u003cp align=\"right\"\u003e(\u003ca href=\"#readme-top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelashu%2Fjson_or_ruby_to_csv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmelashu%2Fjson_or_ruby_to_csv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmelashu%2Fjson_or_ruby_to_csv/lists"}