Skip to content

Instantly share code, notes, and snippets.

@satblip
Last active December 20, 2015 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satblip/6173732 to your computer and use it in GitHub Desktop.
Save satblip/6173732 to your computer and use it in GitHub Desktop.
Merging two JSON
#How I retrieve my JSON :
class CoursesController < ApplicationController
require 'net/https'
require "uri"
require "json"
# List with the courses'information from several sources
def list
# Option 2
require "open-uri"
canvas_api_courses_json = open("https://xxxxx/").read
database_courses = Course.all
database_courses_json = database_courses.to_json
end
end
#First JSON Result
[
{
"course_code":"Basic 101 - 0913",
"name":"Basic 101",
"start_at":"2013-09-16T00:00:00+02:00",
"end_at":"2013-10-13T23:55:00+02:00",
"workflow_state":"available"
},
{"course_code":"Medium 201 - 0913",
"name":"Medium 201",
"start_at":"2013-08-06T16:55:25+02:00",
"end_at":null,
"workflow_state":"available"
}
]
# Secon JSON Result
[
{
"id":1,
"course_id":"Basic 101",
"name":"Basic Level",
"description":"blablabla",
"discipline_id":"1",
"duration":"28",
"created_at":null,
"updated_at":null
},
{
"id":2,
"course_id":"Medium 201",
"name":"Medium Level",
"description":"blablabla",
"discipline_id":"1",
"duration":"28",
"created_at":null,
"updated_at":null
}
]
## Here is the solution :
class CoursesController < ApplicationController
require 'net/https'
require "uri"
require "json"
# List with the courses'information from several sources
def list
# Option final
require "open-uri"
canvas_api_courses_json = open("https://xxxxxxxx").read
database_courses = Course.all
database_courses_json = database_courses.to_json
canvas_api_courses_hash = JSON.parse(canvas_api_courses_json)
database_courses_hash = JSON.parse(database_courses_json)
courses_list = []
canvas_api_courses_hash.each do |json_key_1|
database_courses_hash.each do |json_key_2|
if json_key_2['course_id'] == json_key_1['name']
courses_list.push(json_key_1.merge(json_key_2))
end
end
end
@courses_lis_export = courses_list.to_json
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment