jq使用-在powershell下提取密钥

时间:2018-11-02 13:13:51

标签: json key jq

我在Windows环境中使用jq 1.5修改我从Amazon s3收到的json对象。我有一个有趣的问题。我使用jq提取对象的单键:

{
  "s3_direct_url": "https://fanzo-photos.s3.amazonaws.com/photos/images/034/005/322/screen1.jpg",
  "url": "https://fanzo-photos.s3.amazonaws.com",
  "fields": {
    "key": "photos/images/034/005/322/screen1.jpg",
    "success_action_status": "200",
    "Content-Type": "image/jpeg",
    "acl": "public-read",
    "policy": "eyJleHBpcmF0aW9uIjoiMjAxOC0xMS0wMlQxMzo0NzoxNVoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJmYW56by1waG90b3MifSx7ImtleSI6InBob3Rvcy9pbWFnZXMvMDM0LzAwNS8zMjIvc2NyZWVuMS5qcGcifSx7InN1Y2Nlc3NfYWN0aW9uX3N0YXR1cyI6IjIwMCJ9LHsiQ29udGVudC1UeXBlIjoiaW1hZ2UvanBlZyJ9LHsiYWNsIjoicHVibGljLXJlYWQifSx7IngtYW16LWNyZWRlbnRpYWwiOiJBS0lBSlkzWVRCV1NMQzQ2SFdCQS8yMDE4MTEwMi91cy1lYXN0LTEvczMvYXdzNF9yZXF1ZXN0In0seyJ4LWFtei1hbGdvcml0aG0iOiJBV1M0LUhNQUMtU0hBMjU2In0seyJ4LWFtei1kYXRlIjoiMjAxODExMDJUMTI0NzE1WiJ9XX0=",
    "x-amz-credential": "AKIAJY3YTBWSLC46HWBA/20181102/us-east-1/s3/aws4_request",
    "x-amz-algorithm": "AWS4-HMAC-SHA256",
    "x-amz-date": "20181102T124715Z",
    "x-amz-signature": "52d8246536e8743fba8e7668cb65a08a1142221d54a58676b6ab14e3835482a3"
  },
  "id": 34005322,
  "media_type": "InputMedia"
}

如果我从'fields'对象中提取信息,而键名中没有'-'则可以正常工作。如果我尝试提取名称中带有“-”的键,则会出现以下错误:

 jq: error: amz/0 is not defined at <top-level>, line 1:
.fields.x-amz-credential          
jq: error: credential/0 is not defined at <top-level>, line 1:
.fields.x-amz-credential              
jq: 2 compile errors
exit status 3

更新: 在带有FAQ的提示和“我重建jq命令并在jqplay中对其进行了测试:.fields。“ Content-Type”之后,它按预期方式工作。在powershell下,该变体无法正常工作,因为powershell并未排除配额。

.\jq .fields."Content-Type" jq: error: Type/0 is not defined at <top-level>, line 1: .fields.Content-Type jq: 1 compile error

BR 蒂莫

1 个答案:

答案 0 :(得分:1)

  

有什么提示吗?

是的!如果您在onine jq手册中找不到答案,请检查jq FAQ

  

:如何使用连字符或$或其他键访问键的值   特殊字符吗?为什么.a。[“ $”]会产生语法错误?

     

A:访问键值的基本形式是。[“ KEYNAME”]   其中“ KEYNAME”是任何有效的JSON字符串,但是jq的最新版本   也允许。“ KEYNAME”。

     

使用基本形式可能需要显式使用管道符号,例如   在。[“ a-b”] |。[“ x-y”]中,但是可以缩写为。[“ a-b”] [“ x-y”]。