From 649b7ea4c71df9a9404712ae5f0b029730306dbd Mon Sep 17 00:00:00 2001 From: Shannon Appelcline Date: Wed, 29 Mar 2017 11:11:28 -0700 Subject: [PATCH] Update 4_2__Interlude_Using_JQ.md --- 4_2__Interlude_Using_JQ.md | 41 +++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/4_2__Interlude_Using_JQ.md b/4_2__Interlude_Using_JQ.md index f6a09ed..f935d56 100644 --- a/4_2__Interlude_Using_JQ.md +++ b/4_2__Interlude_Using_JQ.md @@ -1 +1,40 @@ -[TBD] +# Interlude: Using JQ + +> **NOTE:** This is a draft in progress, so that I can get some feedback from early reviewers. It is not yet ready for learning. + +Creating a raw transaction revealed how more complex bitcoin-cli results can't be easily saved into command-line variables. The answer is JQ, which allows you to filter out individual elements from more complex JSON data. + +## Install JQ + +JQ is available from a [Github repository](https://stedolan.github.io/jq/). Just download for Linux, OS X, or Windows, as appropriate. + +Once you've downloaded the binary, you can install it on your system: +``` +$ mv jq-linux64 jq +$ sudo /usr/bin/install -m 0755 -o root -g root -t /usr/local/bin jq +``` +## Use JQ to Capture a JSON Object Key-Values + +In the previous section, the use of `signrawtransaction` offered the first example of not being able to easily capture data into variables due to the use of JSON: +``` +$ bitcoin-cli signrawtransaction $rawtxhex +{ + "hex": "0200000001735dfa1584b930a78ad2c1d6db72dd2a80ae5e5d552ad97e19f1d50d41fdd6d8000000006a47304402202210ce4b2a037da02622c380278cd79fec4e0e016e66f3eb894a2dcbb9ee998f02202cac167e6abdbbf08af139fb7c6b86e9d2e58e5516cd566ae2d54953ead9923b012102111bb978a3c93a00038ae344a1a017d7fee8a9be9d0558b5793ce6f440704a96ffffffff01b0e78604000000001976a914e7c1345fc8f87c68170b3aa798a956c2fe6a9eff88ac00000000", + "complete": true +} +``` +The simplest way to use JQ is to use it as a pipe for output and include a `jq -r .key` argument to capture the raw output key-value. For example: +``` +$ bitcoin-cli signrawtransaction $rawtxhex | jq -r .hex +0200000001735dfa1584b930a78ad2c1d6db72dd2a80ae5e5d552ad97e19f1d50d41fdd6d8000000006a47304402202210ce4b2a037da02622c380278cd79fec4e0e016e66f3eb894a2dcbb9ee998f02202cac167e6abdbbf08af139fb7c6b86e9d2e58e5516cd566ae2d54953ead9923b012102111bb978a3c93a00038ae344a1a017d7fee8a9be9d0558b5793ce6f440704a96ffffffff01b0e78604000000001976a914e7c1345fc8f87c68170b3aa798a956c2fe6a9eff88ac00000000 +``` +With that tool in hand, you can capture information from JSON objects to command-line variables, as usual: +``` +$ signedtx=$(bitcoin-cli signrawtransaction $rawtxhex | jq -r .hex) +$ echo $signedtx +0200000001735dfa1584b930a78ad2c1d6db72dd2a80ae5e5d552ad97e19f1d50d41fdd6d8000000006a47304402202210ce4b2a037da02622c380278cd79fec4e0e016e66f3eb894a2dcbb9ee998f02202cac167e6abdbbf08af139fb7c6b86e9d2e58e5516cd566ae2d54953ead9923b012102111bb978a3c93a00038ae344a1a017d7fee8a9be9d0558b5793ce6f440704a96ffffffff01b0e78604000000001976a914e7c1345fc8f87c68170b3aa798a956c2fe6a9eff88ac00000000 +$ bitcoin-cli sendrawtransaction $signedtx +3f9ccb6e16663e66dc119de1866610cc4f7a83079bfec2abf0598ed3adf10a78 +``` +## Use JQ to Capture a JSON Array Key-Values +