ruby - Print a value of a key from json -
i trying parse json file , extract value of key it, , pass post request. however, not able extract value, when try, prints key instead of value
pfb json file
{ "data":{ "isactivated":true, "userdetails":{ "userid":"52321713-add8-4455-9e0c-426eab923338", "oktaid":"00ub24c5bs6awqybd0h7", "contactid":"7234294092390", "oktasessiontoken":"20111uqaz9-e1yplncxblrcu_zhhzbch2q_j01yiikpyrp5-0e7haqq", "oktasessionid":"102a9q79trqrwek9vhepkp3yq", "apitoken":"f5c95fd8-efc4-497e-8128-51a014de3a9a", "firstname":"judy", "lastname":"test1", "middlename":null, "email":"abc@mailinator.com", "isemployee":true, "pushnotificationstatus":true }, "companydetails":{ "profilescreenbackgroundcolor":"13253d", "companycolor":"7ed321", "companyname":"mobile app demo", "companylogo":"http://", "issso":false } } } pfb hash file:
{"data"=>{"isactivated"=>true, "userdetails"=> {"userid"=>"52321713-add8-4455-9e0c-426eab923338", "oktaid"=>"00ub24c5bs6awqybd0h7", "contactid"=>"0033300001tz8k5aac", "oktasessiontoken"=>"201112ncbw364phojkd4ulzgb1knz9utzpiy2lfdn9tgy_fmgepzmmu", "oktasessionid"=>"102kd-c2yeesnmwr3ykx8qeyg", "apitoken"=>"f8f070e2-e51b-4d69-8b1a-b7b63d25e781", "firstname"=>"judy", "lastname"=>"test1", "middlename"=>nil, "email"=>"judy.test1@mailinator.com", "isemployee"=>true, "pushnotificationstatus"=>true}, "companydetails"=>{"profilescreenbackgroundcolor"=>"13253d", "companycolor"=>"7ed321", "companyname"=>"mobile app demo", "companylogo"=>"https:", "issso"=>false}}} the code below:
i had tried means, not sure missing.
apitoken = restclient.post("https://", {'email'=>arg,'password'=>'abcs','deviceuuid'=>'udid', 'devicetypeid'=>1}.to_json, { "content-type" => 'application/json','buildnumber' => '000','devicetypeid'=>'9'}) puts apitoken puts "**************" puts apitoken["apitoken"] logindetails = json.parse(apitoken) tada = json.parse(logindetails)['data']['apitoken'] puts tada puts logindetails result = logindetails["data"]["apitoken"] puts result puts "**************" logindetails.each |logindetail| puts logindetail puts logindetail["apitoken] puts "**************" end result = logindetails['apitoken'] puts result end the output apitoken instead of value of it. appreciated.
the token under data userdetails apitoken:
json['data']['userdetails']['apitoken'] #=> f5c95fd8-efc4-497e-8128-51a014de3a9a
Comments
Post a Comment