简体   繁体   English

jQuery从JSON中选择动态项目

[英]jq select dynamic items from json

I have JSON like this: 我有这样的JSON:

{
  "photo_807": "Ih2RnaBTg2o.jpg",
  "photo_604": "zodCm9fQgX8.jpg",
  "photo_130": "4Dx-SUNKBw4.jpg",
  "photo_75": "7COWb8ou1qA.jpg",
  "user_id": 100,
  "owner_id": -2435432542783750,
  "access_key": "fc5275423676514042234324265cc3df7607c",
  "post_id": 380435645368865101,
  "date": 14858616848616779856424245814,
  "text": "",
  "height": 417,
  "width": 740,
  "id": 45624575446886886564368555,
  "album_id": -36
}

I want to get only Photo values, from output i want get this: 我只想从输出中获取照片值,我想得到这个:

"photo_807": "Ih2RnaBTg2o.jpg"
"photo_604": "zodCm9fQgX8.jpg"
"photo_130": "4Dx-SUNKBw4.jpg"

Now about my problem, from next JSON file i will get photo items with new names like this: 现在,关于我的问题,从下一个JSON文件中,我将获得具有以下新名称的照片项目:

  "photo_181": "Ih2RnaBTg2o.jpg",
  "photo_583": "zodCm9fQgX8.jpg",
  "photo_975": "4Dx-SUNKBw4.jpg",
  "photo_32": "7COWb8ou1qA.jpg",

How I can get this values from dynamic items photo_* ? 如何从动态项目photo_ *中获得此值?

I try something like this: 我尝试这样的事情:

  cat  ./json3.txt | jq  '.response.items[].attachments[].photo | select(.photo | startswith("photo"))'

But it doesn't work. 但这是行不通的。

When I run : 当我跑步时:

cat  ./json3.txt | jq  '.response.items[].attachments[].photo'  

I got this output with all items: 我得到了所有项目的输出:

{
  "photo_807": "Ih2RnaBTg2o.jpg",
  "photo_604": "zodCm9fQgX8.jpg",
  "photo_130": "4Dx-SUNKBw4.jpg",
  "photo_75": "7COWb8ou1qA.jpg",
  "user_id": 100,
  "owner_id": -2435432542783750,
  "access_key": "fc5275423676514042234324265cc3df7607c",
  "post_id": 380435645368865101,
  "date": 14858616848616779856424245814,
  "text": "",
  "height": 417,
  "width": 740,
  "id": 45624575446886886564368555,
  "album_id": -36
}

Can someone help me? 有人能帮我吗?

Thanks in advance! 提前致谢!

You can use a jq filter as below as tested on jq-play ! 您可以按照以下在jq-play上测试的方式使用jq过滤器!

jq '. | to_entries[] | select(.key | startswith("photo")) | "\(.key) :\(.value)"' json
"photo_807 :Ih2RnaBTg2o.jpg"
"photo_604 :zodCm9fQgX8.jpg"
"photo_130 :4Dx-SUNKBw4.jpg"
"photo_75 :7COWb8ou1qA.jpg"

The idea is to use the to_entries[] built-in, which converts your input into a key-value pair as below. 想法是使用内置的to_entries [] ,它将您的输入转换为键-值对,如下所示。 See below the output of just using to_entries[] 参见下面仅使用to_entries[]的输出

jq '. | to_entries[]' json
{
  "key": "photo_807",
  "value": "Ih2RnaBTg2o.jpg"
}
{
  "key": "photo_604",
  "value": "zodCm9fQgX8.jpg"
}
{
  "key": "photo_130",
  "value": "4Dx-SUNKBw4.jpg"
}
{
  "key": "photo_75",
  "value": "7COWb8ou1qA.jpg"
}
{
  "key": "user_id",
  "value": 100
}
{
  "key": "owner_id",
  "value": -2435432542783750
}
{
  "key": "access_key",
  "value": "fc5275423676514042234324265cc3df7607c"
}
{
  "key": "post_id",
  "value": 380435645368865100
}
{
  "key": "date",
  "value": 14858616848616779000000000000
}
{
  "key": "text",
  "value": ""
}
{
  "key": "height",
  "value": 417
}
{
  "key": "width",
  "value": 740
}
{
  "key": "id",
  "value": 45624575446886885000000000
}
{
  "key": "album_id",
  "value": -36
}

On this output, we are filtering on the .key value which starts with your string, photo in your case, using the built-in startswith() , and printing the both the .key and .value pair for the matching objects. 在此输出中,我们将使用内置的startswith()过滤以字符串开头的.key值(对于您的情况为photo ,并为匹配对象打印.key.value对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM