0

Sorry for novice question, i am a beginner in JSON.

I have the following json:

{
   "destination_addresses" : [ "San Francisco, Californie, États-Unis", "Victoria, BC, Canada" ],
   "origin_addresses" : [ "Vancouver, BC, Canada", "Seattle, Washington, États-Unis" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1 707 km",
                  "value" : 1707145
               },
               "duration" : {
                  "text" : "3 jours 19 heures",
                  "value" : 327471
               },
               "status" : "OK"
            },
           ]
      },

   ],
   "status" : "OK"
}

and i am going to get duration value. i used this line

duration = json["rows"][0]["elements"]["duration"]["text"]

but i am facing with this erorr

 no implicit conversion of String into Integer (TypeError)

1 Answer 1

2

Looking at the example JSON, elements is an array:

"elements" : [...

so needs to be indexed numerically e.g.

duration = json["rows"][0]["elements"][0]["duration"]["text"]

currently the string "duration" is being used to index into the elements array leading to the error you're seeing.

Sign up to request clarification or add additional context in comments.

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.