jq 在 json 对象中为字符串添加前缀

Posted

技术标签:

【中文标题】jq 在 json 对象中为字符串添加前缀【英文标题】:jq to prefix a string in a json object with a string 【发布时间】:2016-09-30 06:49:16 【问题描述】:

我希望使用aws s3api --list-objectsjq 的组合为 Redshift COPY 生成清单文件,如下所示:-

aws s3api list-objects --bucket annalects3 --prefix "DFA/20160926/394007-OMD-Coles/dcm_account394007_impression" --output json --query '"entries": Contents[]."url":"Key"' | jq '.entries[].mandatory = true'

生成如下输出:-

       "entries": [
        
          "mandatory": true,
          "url": "DFA/20160926/394007-OMD-Coles/dcm_account394007_impression_2016092507_20160926_002328_292527438.csv.gz"
        ,
        
          "mandatory": true,
          "url": "DFA/20160926/394007-OMD-Coles/dcm_account394007_impression_2016092508_20160926_020131_292592736.csv.gz"
        ,
        
          "mandatory": true,
          "url": "DFA/20160926/394007-OMD-Coles/dcm_account394007_impression_2016092509_20160926_030312_292502379.csv.gz"
        ,
        
          "mandatory": true,
          "url": "DFA/20160926/394007-OMD-Coles/dcm_account394007_impression_2016092510_20160926_033656_292590227.csv.gz"
           
  ] 

manifest 文件,但是需要以我还没有使用过的存储桶名称为前缀的 URL 对象。输出需要看起来像

   "entries": [
        
          "mandatory": true,
          "url": "s3://mybucket/DFA/20160926/394007-OMD-Coles/dcm_account394007_impression_2016092507_20160926_002328_292527438.csv.gz"
        ,
        
          "mandatory": true,
          "url": "s3://mybucket/DFA/20160926/394007-OMD-Coles/dcm_account394007_impression_2016092508_20160926_020131_292592736.csv.gz"
        ,
        
          "mandatory": true,
          "url": "s3://mybucket/DFA/20160926/394007-OMD-Coles/dcm_account394007_impression_2016092509_20160926_030312_292502379.csv.gz"
        ,
        
          "mandatory": true,
          "url": "s3://mybucket/DFA/20160926/394007-OMD-Coles/dcm_account394007_impression_2016092510_20160926_033656_292590227.csv.gz"
           
  ] 

【问题讨论】:

【参考方案1】:

以下将实现你想要的

aws s3api list-objects \
    --bucket <mybucket> \
    --prefix "<myprefix>" \
    --output json \
    --query '"entries": Contents[]."url":"Key"' \
| jq '.entries[] | .url = "s3://<mybucket>/\(.entries.url)" | .mandatory = true'

我正在使用String interpolation 更新entries[].url

【讨论】:

刚刚意识到对象重复了 24 次。我的意思是 "entries": [ "mandatory": true, "url": "s3://annalects3/DFA/20160926/394007-OMD-Coles/dcm_account394007_impression_2016092607_20160926_235248_292871264.csv.gz" , ] 似乎重复了 24 次。 您确定您的存储桶中没有 24 个项目吗?它对我有用 您可以运行aws s3api list-objects --bucket &lt;mybucket&gt; 并与您获得的结果进行比较。 jq 不会创建比你得到的更多的条目,所以我真的看不出重复项来自哪里,它们必须来自 aws cli aws s3api list-objects --bucket &lt;mybucket&gt; 没有生成任何重复项,而是列出了数组的所有对象。 *** 似乎不允许插入任何图像,或者我可能不知道该怎么做。 哎呀,我已经解决了,我确实遇到了数组的问题,现在它可以工作并且只显示不同的元素,没有重复

以上是关于jq 在 json 对象中为字符串添加前缀的主要内容,如果未能解决你的问题,请参考以下文章

为啥“npm install”在我的“package-lock.json”文件中为我的包添加“node_modules”前缀?

在 AWS AppSync 中为 SQL 查询字符串化 JSON 对象

在jq中将字符串转换为json

如何在 jq 中将 JSON 对象转换为 key=value 格式?

jq json将对象移动到嵌套对象并迭代未知名称/对象数

使用 JQ 展平嵌套的 Json 对象