You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: courses_spec.rb
    path of file: ./repos/yacs/core/spec/requests/api/v5
    
    the code of the file until where you have to start completion: def json_validate_courses(courses=@courses, sections=false, periods=false)
  courses.each_with_index do |course, n|
    ['id', 'name', 'number', 'min_credits', 'max_credits', 'description', 'department_id', 'tags'].each do |field|
      expect(json['courses'][n][field]) .to eq course.attributes[field]
    end

    
def json_validate_courses(courses=@courses, sections=false, periods=false)
  courses.each_with_index do |course, n|
    ['id', 'name', 'number', 'min_credits', 'max_credits', 'description', 'department_id', 'tags'].each do |field|
      expect(json['courses'][n][field]) .to eq course.attributes[field]
    end
    if sections
      course.sections.each_with_index do |section, m|
        expect(json['courses'][n]['sections'][m]['id']) .to eq section.id
        if periods
          expect(json['courses'][n]['sections'][m]['periods'].length) .to eq section.num_periods
        else
          expect(json['courses'][n]['sections'][m]['periods']) .to be_nil
        end
      end
    else
      expect(json['courses'][n]['sections']) .to be_nil
    end