jq json解析器散列字段值 [英] jq json parser hash the field value

查看:126
本文介绍了jq json解析器散列字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件,如下所示:

  [{
macAddress:ac:5f :3e:87:d7:1a,
ip:1.2.3.4
},
{
macAddress:ac:5f:3e:87 :d7:2a,
ip:1.2.3.4
},
{
macAddress:ac:5f:3e:87:d7:3a ,
ip:1.2.3.4
}]

使用 jq 来散列 macAddress 字段,如下所示:

  jq。[] |散列(.macAddress)

我可以定义我自己的散列函数并让 jq 在解析过程中运行散列?



我的散列函数可以很简单,就像使用native linux命令 md5sum

  echo -nmy_salt42:12:20:2e:2b:ca | md5sum 
因此输出json将是



  [{
macAddress:d973ea7c353e78ba1724efbc8054dfdc,
ip:1.2.3.4
},
{
macAddress:d973ea7c353e78ba1724efbc8054d2er,
ip:1.2.3.4
},
{
macAddress:d973ea7c353e78ba2324efbc8054d123 ,
ip:1.2.3.4
}]


解决方案


我的预期散列函数可以很简单,就像使用本机linux命令 md5sum


这保留原生并可能适合;

调用:



  jq -c。[]$ jsonfile| 
读取-r jsonline; do
hashmac =$(jq --arg mysalt$ mysalt-s -j'
。[] |\($ mysalt)+ .macAddress'<< $ jsonline|
md5sum | cut -d''-f1)
jq --arg hashmac$ hashmac-s -r'
。[] | .macAddress | =\($ hashmac)''<<<$ jsonline
完成



$ hr
$ b

示例文件 - / tmp / testfile:



  [{
macAddress:ac:5f:3e:87:d7:1a,
ip:1.2.3.4
},
{
macAddress:ac:5f:3e:87:d7:2a,
ip:1.2.3.4
},
{
macAddress :ac:5f:3e:87:d7:3a,
ip:1.2.3.4
},
{
macAddress:42 :12:20:2e:2b:ca,
ip:1.2.3.4
}]



结果输出:



  {
macAddress:1f960fe4d24684ca44e5e67b6259362c,
ip:1.2.3.4
}
{
macAddress:3527422754ecbfdd01d48b17fce87842,
ip:1.2.3.4


macAddress:9bc8da72324448c3032a20fb67a31466,
ip:1.2.3.4
}
{
macAddress :d973ea7c353e78ba1724efbc8054dfdc,
ip:1.2.3.4
}



< h2>评论:


  • -j 会导致jq不输出换行符,相当于您的 echo - n 例子


  • 这个例子中的变量作为字符串发送给 jq 使用 -arg ,并引用为\($ var) ,而不是直接转义变量,例如:


    • \($ mysalt)+。 macAddress'(jq变量)



      不是:

    • code>'$ mysalt'+ .macAddress'
      (直接shell替换)

  • 这个例子使用 cut -d''-f1 来修剪 - ,但可能有更好的方法




备用:



jq --arg hashmac$ hashmac-s -r'。[] | =。 + {hashAddress:\($ hashmac)}'



将附加json

  [
{
macAddress:ac:5f:3e:87:d7:1a,
ip :1.2.3.4,
hashAddress:1f960fe4d24684ca44e5e67b6259362c
}
]

等。


I have a JSON file as the below:

[{
  "macAddress": "ac:5f:3e:87:d7:1a",
  "ip": "1.2.3.4"
},
{
  "macAddress": "ac:5f:3e:87:d7:2a",
  "ip": "1.2.3.4"
},
{
  "macAddress": "ac:5f:3e:87:d7:3a",
  "ip": "1.2.3.4"
}]

use jq to hash the macAddress field like so:

jq .[] | hash(.macAddress)

Can I define my own hash function and let jq to run the hash during the parsing process?

My expected hash function can be simple as using native linux command md5sum

echo -n "my_salt""42:12:20:2e:2b:ca" | md5sum
d973ea7c353e78ba1724efbc8054dfdc -

So the output json will be

[{
  "macAddress": "d973ea7c353e78ba1724efbc8054dfdc",
  "ip": "1.2.3.4"
},
{
  "macAddress": "d973ea7c353e78ba1724efbc8054d2er",
  "ip": "1.2.3.4"
},
{
  "macAddress": "d973ea7c353e78ba2324efbc8054d123",
  "ip": "1.2.3.4"
}]

解决方案

My expected hash function can be simple as using native linux command md5sum

This stays native and might be suitable;

Invocation :

jq -c .[] "$jsonfile" |
while read -r jsonline ; do
  hashmac="$(jq --arg mysalt "$mysalt" -s -j '
        .[] | "\($mysalt)" + .macAddress' <<<"$jsonline" |
      md5sum | cut -d ' ' -f1)"
  jq --arg hashmac "$hashmac" -s -r '
      .[] | .macAddress |= "\($hashmac)"' <<<"$jsonline"
done


Example file - /tmp/testfile:

[{
  "macAddress": "ac:5f:3e:87:d7:1a",
  "ip": "1.2.3.4"
},
{
  "macAddress": "ac:5f:3e:87:d7:2a",
  "ip": "1.2.3.4"
},
{
  "macAddress": "ac:5f:3e:87:d7:3a",
  "ip": "1.2.3.4"
},
{
 "macAddress": "42:12:20:2e:2b:ca",
 "ip": "1.2.3.4"
}]

Result Output:

{
  "macAddress": "1f960fe4d24684ca44e5e67b6259362c",
  "ip": "1.2.3.4"
}
{
  "macAddress": "3527422754ecbfdd01d48b17fce87842",
  "ip": "1.2.3.4"
}
{
  "macAddress": "9bc8da72324448c3032a20fb67a31466",
  "ip": "1.2.3.4"
}
{
  "macAddress": "d973ea7c353e78ba1724efbc8054dfdc",
  "ip": "1.2.3.4"
}

Comments:

  • -j causes jq to not output a newline, equivalent to your echo -n example

  • Variables in this example are sent to jq as strings using -arg, and referenced as "\($var)" as opposed to escaping the variable directly, for example:

    • "\($mysalt)" + .macAddress' (jq variable)

      Instead of:

    • "'"$mysalt"'" + .macAddress' (direct shell substitution)

  • This example uses cut -d ' ' -f1 to trim off the -, but there's probably a better way

Alternate:

jq --arg hashmac "$hashmac" -s -r '.[] |= . + {"hashAddress":"\($hashmac)"}'

Would append the json

[
  {
    "macAddress": "ac:5f:3e:87:d7:1a",
    "ip": "1.2.3.4",
    "hashAddress": "1f960fe4d24684ca44e5e67b6259362c"
  }
]

etc.

这篇关于jq json解析器散列字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆