How to parse Square response nested key values in Ruby 2.2.10How to get a table of records from Square's transaction hash?How to generate a random string in RubyHow to get a random number in RubyHow to write a switch statement in RubyHow to convert a string to lower or upper case in RubyCheck if a value exists in an array in RubyHow do I get the current absolute URL in Ruby on Rails?How to write to file in Ruby?How to remove a key from Hash and get the remaining hash in Ruby/Rails?How to check whether a string contains a substring in Ruby?How to install a specific version of a ruby gem?

How did the Axis intend to hold the Caucasus?

Why would a pilot use ailerons for countering asymmetric thrust in mid-flight?

Introducing Tetronogram!

Why radial coordinate of a particle must decrease continuously once it is inside the Schwarzschild radius?

reconstruction filter - How does it actually work?

Is it okay for me to decline a project on ethical grounds?

How should Scrum and Kanban teams track Continuous Improvement activities?

How can I kill my goat?

Are the named pipe created by `mknod` and the FIFO created by `mkfifo` equivalent?

Can I change the license of a forked project to the MIT if the license of the parent project has changed from the GPL to the MIT?

How could Nomadic scholars effectively memorize libraries worth of information

Is it error of law to judge on less relevant case law when there is much more relevant one?

Polyhedra, Polyhedron, Polytopes and Polygon

Name These Animals

Wand of the War Mage spellcasting focus and bonus interaction with multiclassing

Copying an existing HTML page and use it, is that against any copyright law?

Is this photo showing a woman standing in the nude before teenagers real?

Sci-fi change: Too much or Not enough

Irreducible factors of primitive permutation group representation

What is "aligned sequences" and "consensus sequence" in the context of sequence logo? How to compute these?

How do you pronounce "Hain"?

Do the books ever say oliphaunts aren’t elephants?

Assuring luggage isn't lost with short layover

Anti-cheating: should there be a limit to a number of toilet breaks per game per player?



How to parse Square response nested key values in Ruby 2.2.10


How to get a table of records from Square's transaction hash?How to generate a random string in RubyHow to get a random number in RubyHow to write a switch statement in RubyHow to convert a string to lower or upper case in RubyCheck if a value exists in an array in RubyHow do I get the current absolute URL in Ruby on Rails?How to write to file in Ruby?How to remove a key from Hash and get the remaining hash in Ruby/Rails?How to check whether a string contains a substring in Ruby?How to install a specific version of a ruby gem?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















How would you get the value of the last_4 key form this hash?



I have a response nonce from a Square transaction. I pass the response through Ajax to Ruby where it comes out as a parameter string. Iv'e tried converting the string to a hash. I've also tried JSON.parse.



This is an actual response nonce from a Square sandbox transaction. I've truncated some of the IDs and replaced others. Also i put in carriage returns to make it more human readable:



:transaction=>
:id=>"smqfzS00qbp1lOy...",
:location_id=>"CBASE...",
:created_at=>"2019-02-19T19:45:18Z",
:tenders=>[
:id=>"34670bfa-9d09-406a-910c-9c3e8ab82321",
:location_id=>"CBASE...",
:transaction_id=>"smqfzS00qbp1lOy...",
:created_at=>"2019-02-19T19:45:18Z",
:note=>"Online Transaction",
:amount_money=>
:amount=>65000,
:currency=>"USD"
,
:type=>"CARD",
:card_details=>
:status=>"CAPTURED",
:card=>
:card_brand=>"VISA",
:last_4=>"9999",
:fingerprint=>"22737c9b012a..."
,
:entry_method=>"KEYED"

],
:product=>"EXTERNAL_API"




I have it as a string from an ajax call and convert it to a hash or parse it:



sqresp = Hash.new(square_resp) # this works
sqresp = JSON.parse(square_resp) # this fails


I've tried pulling out just the id, which is the first nested key:



sqresp[:transaction] # this works but I get the whole string


But if I go any deeper it fails:



sqresp[:transaction][:id] # this fails
sqresp[:transaction[:id]] # kinda makes sense but fails
sqresp[:transaction][:tenders][:amount_money][:card_details][:card][:last_4] # of course this fails too,it's just a deeper scrape.


It would be great to have a Ruby method for breaking out all the Square key/values but with an example of how to pull out last_4, I can do the rest.



Thanks.










share|improve this question
























  • Your first sentence is misleading because what follows is not a hash, but, as you go on to explain, is a string. You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. (I know nothing about either.) If you do that, more tags would be called for.

    – Cary Swoveland
    Mar 26 at 20:11












  • If str is your string (I treated it as the body of a heredoc), h = eval(str) (or instance_eval(str)) produces the hash, after which h[:transaction][:tenders].first[:card_details][:card][:last_4] #=> "9999". Hash.new(str) merely creates an empty hash with str as its default value.

    – Cary Swoveland
    Mar 26 at 20:23












  • RE: You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. nonce is the term for the response that comes from Square: docs.connect.squareup.com/payments/sqpaymentform/… Square returns the nonce as a string that they populate into the value of a hidden input field. I passed that field value to Ruby through a JSON string in an AJAX call. I pasted the actual string - exception I added carriage returns to emphasize the structure. The tags are appropriate for search purposes.

    – whatbox
    Mar 28 at 14:48

















0















How would you get the value of the last_4 key form this hash?



I have a response nonce from a Square transaction. I pass the response through Ajax to Ruby where it comes out as a parameter string. Iv'e tried converting the string to a hash. I've also tried JSON.parse.



This is an actual response nonce from a Square sandbox transaction. I've truncated some of the IDs and replaced others. Also i put in carriage returns to make it more human readable:



:transaction=>
:id=>"smqfzS00qbp1lOy...",
:location_id=>"CBASE...",
:created_at=>"2019-02-19T19:45:18Z",
:tenders=>[
:id=>"34670bfa-9d09-406a-910c-9c3e8ab82321",
:location_id=>"CBASE...",
:transaction_id=>"smqfzS00qbp1lOy...",
:created_at=>"2019-02-19T19:45:18Z",
:note=>"Online Transaction",
:amount_money=>
:amount=>65000,
:currency=>"USD"
,
:type=>"CARD",
:card_details=>
:status=>"CAPTURED",
:card=>
:card_brand=>"VISA",
:last_4=>"9999",
:fingerprint=>"22737c9b012a..."
,
:entry_method=>"KEYED"

],
:product=>"EXTERNAL_API"




I have it as a string from an ajax call and convert it to a hash or parse it:



sqresp = Hash.new(square_resp) # this works
sqresp = JSON.parse(square_resp) # this fails


I've tried pulling out just the id, which is the first nested key:



sqresp[:transaction] # this works but I get the whole string


But if I go any deeper it fails:



sqresp[:transaction][:id] # this fails
sqresp[:transaction[:id]] # kinda makes sense but fails
sqresp[:transaction][:tenders][:amount_money][:card_details][:card][:last_4] # of course this fails too,it's just a deeper scrape.


It would be great to have a Ruby method for breaking out all the Square key/values but with an example of how to pull out last_4, I can do the rest.



Thanks.










share|improve this question
























  • Your first sentence is misleading because what follows is not a hash, but, as you go on to explain, is a string. You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. (I know nothing about either.) If you do that, more tags would be called for.

    – Cary Swoveland
    Mar 26 at 20:11












  • If str is your string (I treated it as the body of a heredoc), h = eval(str) (or instance_eval(str)) produces the hash, after which h[:transaction][:tenders].first[:card_details][:card][:last_4] #=> "9999". Hash.new(str) merely creates an empty hash with str as its default value.

    – Cary Swoveland
    Mar 26 at 20:23












  • RE: You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. nonce is the term for the response that comes from Square: docs.connect.squareup.com/payments/sqpaymentform/… Square returns the nonce as a string that they populate into the value of a hidden input field. I passed that field value to Ruby through a JSON string in an AJAX call. I pasted the actual string - exception I added carriage returns to emphasize the structure. The tags are appropriate for search purposes.

    – whatbox
    Mar 28 at 14:48













0












0








0








How would you get the value of the last_4 key form this hash?



I have a response nonce from a Square transaction. I pass the response through Ajax to Ruby where it comes out as a parameter string. Iv'e tried converting the string to a hash. I've also tried JSON.parse.



This is an actual response nonce from a Square sandbox transaction. I've truncated some of the IDs and replaced others. Also i put in carriage returns to make it more human readable:



:transaction=>
:id=>"smqfzS00qbp1lOy...",
:location_id=>"CBASE...",
:created_at=>"2019-02-19T19:45:18Z",
:tenders=>[
:id=>"34670bfa-9d09-406a-910c-9c3e8ab82321",
:location_id=>"CBASE...",
:transaction_id=>"smqfzS00qbp1lOy...",
:created_at=>"2019-02-19T19:45:18Z",
:note=>"Online Transaction",
:amount_money=>
:amount=>65000,
:currency=>"USD"
,
:type=>"CARD",
:card_details=>
:status=>"CAPTURED",
:card=>
:card_brand=>"VISA",
:last_4=>"9999",
:fingerprint=>"22737c9b012a..."
,
:entry_method=>"KEYED"

],
:product=>"EXTERNAL_API"




I have it as a string from an ajax call and convert it to a hash or parse it:



sqresp = Hash.new(square_resp) # this works
sqresp = JSON.parse(square_resp) # this fails


I've tried pulling out just the id, which is the first nested key:



sqresp[:transaction] # this works but I get the whole string


But if I go any deeper it fails:



sqresp[:transaction][:id] # this fails
sqresp[:transaction[:id]] # kinda makes sense but fails
sqresp[:transaction][:tenders][:amount_money][:card_details][:card][:last_4] # of course this fails too,it's just a deeper scrape.


It would be great to have a Ruby method for breaking out all the Square key/values but with an example of how to pull out last_4, I can do the rest.



Thanks.










share|improve this question
















How would you get the value of the last_4 key form this hash?



I have a response nonce from a Square transaction. I pass the response through Ajax to Ruby where it comes out as a parameter string. Iv'e tried converting the string to a hash. I've also tried JSON.parse.



This is an actual response nonce from a Square sandbox transaction. I've truncated some of the IDs and replaced others. Also i put in carriage returns to make it more human readable:



:transaction=>
:id=>"smqfzS00qbp1lOy...",
:location_id=>"CBASE...",
:created_at=>"2019-02-19T19:45:18Z",
:tenders=>[
:id=>"34670bfa-9d09-406a-910c-9c3e8ab82321",
:location_id=>"CBASE...",
:transaction_id=>"smqfzS00qbp1lOy...",
:created_at=>"2019-02-19T19:45:18Z",
:note=>"Online Transaction",
:amount_money=>
:amount=>65000,
:currency=>"USD"
,
:type=>"CARD",
:card_details=>
:status=>"CAPTURED",
:card=>
:card_brand=>"VISA",
:last_4=>"9999",
:fingerprint=>"22737c9b012a..."
,
:entry_method=>"KEYED"

],
:product=>"EXTERNAL_API"




I have it as a string from an ajax call and convert it to a hash or parse it:



sqresp = Hash.new(square_resp) # this works
sqresp = JSON.parse(square_resp) # this fails


I've tried pulling out just the id, which is the first nested key:



sqresp[:transaction] # this works but I get the whole string


But if I go any deeper it fails:



sqresp[:transaction][:id] # this fails
sqresp[:transaction[:id]] # kinda makes sense but fails
sqresp[:transaction][:tenders][:amount_money][:card_details][:card][:last_4] # of course this fails too,it's just a deeper scrape.


It would be great to have a Ruby method for breaking out all the Square key/values but with an example of how to pull out last_4, I can do the rest.



Thanks.







ruby string hash square






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 at 15:01







whatbox

















asked Mar 26 at 19:35









whatboxwhatbox

32 bronze badges




32 bronze badges












  • Your first sentence is misleading because what follows is not a hash, but, as you go on to explain, is a string. You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. (I know nothing about either.) If you do that, more tags would be called for.

    – Cary Swoveland
    Mar 26 at 20:11












  • If str is your string (I treated it as the body of a heredoc), h = eval(str) (or instance_eval(str)) produces the hash, after which h[:transaction][:tenders].first[:card_details][:card][:last_4] #=> "9999". Hash.new(str) merely creates an empty hash with str as its default value.

    – Cary Swoveland
    Mar 26 at 20:23












  • RE: You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. nonce is the term for the response that comes from Square: docs.connect.squareup.com/payments/sqpaymentform/… Square returns the nonce as a string that they populate into the value of a hidden input field. I passed that field value to Ruby through a JSON string in an AJAX call. I pasted the actual string - exception I added carriage returns to emphasize the structure. The tags are appropriate for search purposes.

    – whatbox
    Mar 28 at 14:48

















  • Your first sentence is misleading because what follows is not a hash, but, as you go on to explain, is a string. You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. (I know nothing about either.) If you do that, more tags would be called for.

    – Cary Swoveland
    Mar 26 at 20:11












  • If str is your string (I treated it as the body of a heredoc), h = eval(str) (or instance_eval(str)) produces the hash, after which h[:transaction][:tenders].first[:card_details][:card][:last_4] #=> "9999". Hash.new(str) merely creates an empty hash with str as its default value.

    – Cary Swoveland
    Mar 26 at 20:23












  • RE: You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. nonce is the term for the response that comes from Square: docs.connect.squareup.com/payments/sqpaymentform/… Square returns the nonce as a string that they populate into the value of a hidden input field. I passed that field value to Ruby through a JSON string in an AJAX call. I pasted the actual string - exception I added carriage returns to emphasize the structure. The tags are appropriate for search purposes.

    – whatbox
    Mar 28 at 14:48
















Your first sentence is misleading because what follows is not a hash, but, as you go on to explain, is a string. You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. (I know nothing about either.) If you do that, more tags would be called for.

– Cary Swoveland
Mar 26 at 20:11






Your first sentence is misleading because what follows is not a hash, but, as you go on to explain, is a string. You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. (I know nothing about either.) If you do that, more tags would be called for.

– Cary Swoveland
Mar 26 at 20:11














If str is your string (I treated it as the body of a heredoc), h = eval(str) (or instance_eval(str)) produces the hash, after which h[:transaction][:tenders].first[:card_details][:card][:last_4] #=> "9999". Hash.new(str) merely creates an empty hash with str as its default value.

– Cary Swoveland
Mar 26 at 20:23






If str is your string (I treated it as the body of a heredoc), h = eval(str) (or instance_eval(str)) produces the hash, after which h[:transaction][:tenders].first[:card_details][:card][:last_4] #=> "9999". Hash.new(str) merely creates an empty hash with str as its default value.

– Cary Swoveland
Mar 26 at 20:23














RE: You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. nonce is the term for the response that comes from Square: docs.connect.squareup.com/payments/sqpaymentform/… Square returns the nonce as a string that they populate into the value of a hidden input field. I passed that field value to Ruby through a JSON string in an AJAX call. I pasted the actual string - exception I added carriage returns to emphasize the structure. The tags are appropriate for search purposes.

– whatbox
Mar 28 at 14:48





RE: You might be better off showing your return value from the Square Transaction ("nounce"?) or from Ajax. nonce is the term for the response that comes from Square: docs.connect.squareup.com/payments/sqpaymentform/… Square returns the nonce as a string that they populate into the value of a hidden input field. I passed that field value to Ruby through a JSON string in an AJAX call. I pasted the actual string - exception I added carriage returns to emphasize the structure. The tags are appropriate for search purposes.

– whatbox
Mar 28 at 14:48












1 Answer
1






active

oldest

votes


















0














Cary, that worked!



sqresp = instance_eval(square_resp)
sqresp[:transaction][:tenders].first[:card_details][:card][:last_4]



Thanks!
There's no option to vote for your comment.






share|improve this answer

























  • Revised. You are correct.

    – whatbox
    Mar 28 at 14:30










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365027%2fhow-to-parse-square-response-nested-key-values-in-ruby-2-2-10%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Cary, that worked!



sqresp = instance_eval(square_resp)
sqresp[:transaction][:tenders].first[:card_details][:card][:last_4]



Thanks!
There's no option to vote for your comment.






share|improve this answer

























  • Revised. You are correct.

    – whatbox
    Mar 28 at 14:30















0














Cary, that worked!



sqresp = instance_eval(square_resp)
sqresp[:transaction][:tenders].first[:card_details][:card][:last_4]



Thanks!
There's no option to vote for your comment.






share|improve this answer

























  • Revised. You are correct.

    – whatbox
    Mar 28 at 14:30













0












0








0







Cary, that worked!



sqresp = instance_eval(square_resp)
sqresp[:transaction][:tenders].first[:card_details][:card][:last_4]



Thanks!
There's no option to vote for your comment.






share|improve this answer















Cary, that worked!



sqresp = instance_eval(square_resp)
sqresp[:transaction][:tenders].first[:card_details][:card][:last_4]



Thanks!
There's no option to vote for your comment.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 14:28

























answered Mar 26 at 20:33









whatboxwhatbox

32 bronze badges




32 bronze badges












  • Revised. You are correct.

    – whatbox
    Mar 28 at 14:30

















  • Revised. You are correct.

    – whatbox
    Mar 28 at 14:30
















Revised. You are correct.

– whatbox
Mar 28 at 14:30





Revised. You are correct.

– whatbox
Mar 28 at 14:30








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365027%2fhow-to-parse-square-response-nested-key-values-in-ruby-2-2-10%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해