lundi 3 février 2020

nested array ruby TypeError: no implicit conversion of Hash into Integer

def get_att(webinar_id)
  finalArray = []
    api_service = ApiService.new
    response = api_service.get_attendees(webinar_id)
    json = JSON.parse(response.body)

  for x in json['_embedded']['attendeeParticipationResponses']
    first_name = json['_embedded']['attendeeParticipationResponses'][x]['firstName']
    last_name = json['_embedded']['attendeeParticipationResponses'][x]['lastName']
    email = json['_embedded']['attendeeParticipationResponses'][x]['email']
    finalArray << [first_name, last_name, email]
  end

  # x = 2
  # first_name = json['_embedded']['attendeeParticipationResponses'][x]['firstName']
  # last_name = json['_embedded']['attendeeParticipationResponses'][x]['lastName']
  # email = json['_embedded']['attendeeParticipationResponses'][x]['email']
  # finalArray << [first_name, last_name, email]

    Rails.logger.info(finalArray)
end

This is the method (sorry the indenting is weird when pasted into stack overflow, but its normal in actual code). The idea is to return an array of arrays with all of the people and their 3 tags. I know that the JSON parse data is working because the commented out code works perfectly and I can change x assignment and it runs. The for loop also works because it counts 15 responses in json['_embedded']['attendeeParticipationResponses'] when I add a counter. So I know the 15 people are there and I can add them one by one into the final array (which I got to hold multiple arrays by the way), but for some reason the second I put it in the for loop I get the weird error which fully reads:

TypeError: no implicit conversion of Hash into Integer from /Users/josh/Documents/GitHub/schools_health/lib/go_to_webinar/to_pipeline.rb:19:in `[]'

Aucun commentaire:

Enregistrer un commentaire