如何使用jsawk解析bash中的多维JSON数组?

时间:2016-01-08 07:31:07

标签: json bash multidimensional-array jsawk

我有一个如下所示的数组。我想将整个数据解析为我的bash数组。 所以我可以调用第一个" JSON addressLineOne"来自$ {bashaddr [0]},等等。

[
{
"id":"f0c546d5-0ce4-55ee-e043-516e0f0afdc1",
"cardType":"WMUSGESTORECARD",
"lastFour":"1682",
"cardExpiryDate":"2012-01-16",
"firstName":"robert",
"lastName":"robishaw",
"addressLineOne":"Apt venue",
"addressLineTwo":"",
"city":"oakdale",
"state":"CT",
"postalCode":"06370",
"phone":"534534",
"isDefault":false
},
{
"id":"f0c546d5-0ce0-55ee-e043-516e0f0afdc1",
"cardType":"MASTERCARD",
"lastFour":"2731",
"cardExpiryDate":"2009-08-31",
"firstName":"robert",
"lastName":"robishaw",
"addressLineOne":"119 maple ave.",
"addressLineTwo":"",
"city":"uncasville",
"state":"CT",
"postalCode":"06382",
"phone":"7676456",
"isDefault":false
},
{
"id":"f0c546d5-0ce2-55ee-e043-516e0f0afdc1",
"cardType":"MASTERCARD",
"lastFour":"6025",
"cardExpiryDate":"2011-08-31",
"firstName":"robert",
"lastName":"robishaw",
"addressLineOne":"Angeline Street",
"addressLineTwo":"",
"city":"oakdale",
"state":"CT",
"postalCode":"06370",
"phone":"7867876",
"isDefault":false
}

我试过这样的话:

#!/bin/bash
addressLineOne="$(echo $card | jsawk 'return this.addressLineOne')"

但它给了我整个地址:

["address 1","address 2","address 3"]

谢谢。

2 个答案:

答案 0 :(得分:1)

我在阅读评论之前写了下面的答案,但这与@ 4ae1e1提供的答案完全相同,只是我没有放`include "adder.v" module processData(dataOut0, dataOut1, dataIn0, dataIn1, key0, key1, key2, key3, sumIn, sumOut, delta, clk, rst, finish); output reg [1:32] dataOut0, dataOut1, sumOut; output reg finish; input [1:32] dataIn0, dataIn1, key0, key1, key2, key3, sumIn, delta; input clk, rst; reg [1:32] in1, in2, first, second, third, tsum; wire [1:32] out; integer cnt; adder add(out, in1, in2); always @(posedge clk or negedge rst) begin if(!rst) begin cnt = 0; finish = 0; //sumOut = 0; end else begin if(cnt == 0) begin in1 = sumIn; in2 = delta; sumOut = out; cnt = cnt + 1; end else if(cnt == 1) begin in1 = dataIn1 << 4; in2 = key0; first = out; cnt = cnt + 1; end else if(cnt == 2) begin in1 = dataIn1; in2 = sumOut; second = out; cnt = cnt + 1; end else if(cnt == 3) begin in1 = dataIn1 >> 5; in2 = key1; third = out; cnt = cnt + 1; end else if(cnt == 4) begin tsum = first ^ second ^ third; cnt = cnt + 1; end else if(cnt == 5) begin in1 = tsum; in2 = dataIn0; dataOut0 = out; cnt = cnt + 1; end else if(cnt == 6) begin in1 = dataOut0 << 4; in2 = key2; first = out; cnt = cnt + 1; end else if(cnt == 7) begin in1 = dataOut0; in2 = sumOut; second = out; cnt = cnt + 1; end else if(cnt == 8) begin in1 = dataOut0 >> 5; in2 = key3; third = out; cnt = cnt + 1; end else if(cnt == 9) begin tsum = first ^ second ^ third; cnt = cnt + 1; end else if(cnt == 10) begin in1 = tsum; in2 = dataIn1; dataOut1 = out; cnt = cnt + 1; end else if(cnt == 11) begin finish = 1; end end end endmodule 标签以防你希望值保持引用(例如通过这个作为其他地方的论据。)

我知道这不是jsawk,但请考虑-r

jq

要访问特定值,您可以将记录号放在方括号中(从第一个地址的0开始,依此类推)。例如,获取第三条记录的地址:

jq '.[].addressLineOne' yourfile.txt

要详细了解jq '.[2].addressLineOne' yourfile.txt和高级用途,请查看:http://jqplay.org

答案 1 :(得分:0)

您需要做的是利用-a开关进行一些后处理并过滤输出数组,如下所示: jsawk 'return this.addressLineOne' -a 'return this[0]' 从文档中:

-b <script> | -a <script>
      Run the specified snippet of JavaScript before (-b) or after (-a)
      processing JSON input. The `this` object is set to the whole JSON
      array or object. This is used to preprocess (-b) or postprocess
      (-a) the JSON array before or after the main script is applied.
      This option can be specified multiple times to define multiple
      before/after scripts, which will be applied in the order they
      appeared on the command line.