使用JQ从其他目录获取json字段值 [英] Get json field value with JQ from different directory

查看:136
本文介绍了使用JQ从其他目录获取json字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题可能不正确,因为我实际上不确定这在哪里失败.我有一个在一个目录中运行的bash脚本,还有一个JSON文件,我需要来自另一个目录中的值.我要将值从外部目录复制到当前目录中的相同JSON文件中.

Title may be incorrect as I'm not actually sure where this is failing. I have a bash script running in one directory, and a JSON file I need a value from in a different directory. I want to copy the value from the external directory into an identical JSON file in the current directory.

我正在使用jq来获取值,但是我无法弄清楚如何从脚本运行所在的目录之外的目录中获取.

I'm using jq to grab the value, but I can't figure out how to grab from a directory other than the one the script is running in.

文件结构的相关位如下;

The relevant bits of file structure are as follows;

cloudformation
  - parameters_v13.json
environment_files
  - prepare_stack_files.json (the script this is run from)
  - directory, changes based on where the script is pointed
      - created directory where created files are being output
         - GREPNAME_parameters.json

我感兴趣的JSON文件块看起来像这样;

The chunk of the JSON file I'm interested in looks like this;

[ 
{
   "ParameterKey": "RTSMEMAIL",
   "ParameterValue": "secretemail"
  }
]

该脚本需要从cloudformation/parameters_v13.json获取"secrettemail",并将其粘贴到GREPNAME_parameters.json文件中匹配的RTSMEMAIL字段中.

The script needs to get the "secretemail" from cloudformation/parameters_v13.json and paste it into the matching RTSMEMAIL field in the GREPNAME_parameters.json file.

我一直在尝试以下操作,但是没有运气-没有任何输出.也没有错误消息,只是空白输出.我知道GREPNAME路径是正确的,因为它已在其他地方使用,没有问题.

I've been attempting the following with no luck - nothing is output. No error message either, just blank output. I know the GREPNAME path is correct because it's used elsewhere with no issues.

jq --arg email "$EMAIL" '(.[] | select(.ParameterKey == "RTSMEMAIL") | .ParameterValue) |= $email' ../cloudformation/parameters_v13.json | sponge ${GREPNAME}_parameters.json

推荐答案

jq过滤器应该可以帮助您获取secretmail字符串

This jq filter should help you get secretmail string

jq '.[] | select(.ParameterKey=="RTSMEMAIL") | .ParameterValue' json
"secretemail"

为原始输出添加一个-r文件,以删除该值周围的引号

Add a -r file for raw output to remove quotes around the value

jq -r '.[] | select(.ParameterKey=="RTSMEMAIL") | .ParameterValue' json
secretemail

-原始输出/-r:

--raw-output / -r:

使用此选项,如果过滤器的结果是字符串,则将其直接写入标准输出,而不是将其格式化为带引号的JSON字符串.这对于使jq过滤器与非基于JSON的系统进行通信很有用.

With this option, if the filter’s result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems.

正如我所看到的,您正在尝试将args传递给jq过滤器,要进行提取,您可以先通过在bash

As I could see it you are trying to pass args to jq filter, for extraction you can do something first by setting the variable in bash

email="RTSMEMAIL"

现在将其传递给过滤器

jq --arg email "$email" -r '.[] | select(.ParameterKey==$email) | .ParameterValue' json 
secretemail

现在要将从parameters_v13.json文件获得的字符串替换为您的GREPNAME_parameters.json,请执行以下步骤:-

Now to replace the string obtained from parameters_v13.json file to your GREPNAME_parameters.json do the following steps:-

首先将第一个文件的结果存储在变量中以供以后使用,我已经使用该文件将其提取为json,这实际上将您的parameters_v13.json文件指向了另一个路径.

First storing the result from the first file in a variable to re-use later, I have used the file to extract as json, this actually points your parameters_v13.json file in another path.

replacementValue=$(jq --arg email "$email" -r '.[] | select(.ParameterKey==$email) | .ParameterValue' json)

现在$replacementValue将保存要更新到另一个文件的secretmail.如前所述,GREPNAME_parameters.json的语法与第一个文件相似.像下面这样

now the $replacementValue will hold the secretmail which you want to update to another file. As you have indicated previously GREPNAME_parameters.json has a similar syntax as of the first file. Something like below,

$ cat GREPNAME_parameters.json
[ 
{
   "ParameterKey": "SOMEJUNK",
   "ParameterValue": "somejunkvalue"
  }
]

现在,我了解您的意图是将上述文件中的"ParameterValue"替换为从其他文件获得的值.为此,

Now I understand your intention is replace "ParameterValue" from the above file to the value obtained from the other file. To achieve that,

jq --arg replace "$replacementValue" '.[] | .ParameterValue = $replace' GREPNAME_parameters.json
{
  "ParameterKey": "SOMEJUNK",
  "ParameterValue": "secretemail"
}

然后可以将此输出写入一个临时文件,并将其作为GREPNAME_parameters.json移回.希望这能回答您的问题.

You can then write this output to the a temp file and move it back as the GREPNAME_parameters.json. Hope this answers your question.

这篇关于使用JQ从其他目录获取json字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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