首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果使用jq存在另一个值,则返回该值

如果使用jq存在另一个值,则返回该值
EN

Stack Overflow用户
提问于 2022-11-16 18:05:46
回答 2查看 32关注 0票数 0

我有一个json片段如下所示。如果版本键有值,我希望返回名称键的值和版本键的值。预期输出为"name":"value“,"version”:"value“

最好有一个解决方案与grep或jq。

代码语言:javascript
运行
复制
node ~/wappalyzer/src/drivers/npm/cli.js https://youtube.com | jq .

结果:

代码语言:javascript
运行
复制
{
  "urls": {
    "https://youtube.com/": {
      "status": 301
    },
    "https://www.youtube.com/": {
      "status": 200
    }
  },
  "technologies": [
    {
      "slug": "youtube",
      "name": "YouTube",
      "description": "YouTube is a video sharing service where users can create their own profile, upload videos, watch, like and comment on other videos.",
      "confidence": 100,
      "version": null,
      "icon": "YouTube.png",
      "website": "http://www.youtube.com",
      "cpe": null,
      "categories": [
        {
          "id": 14,
          "slug": "video-players",
          "name": "Video players"
        }
      ],
      "rootPath": true
    },
    {
      "slug": "polymer",
      "name": "Polymer",
      "description": null,
      "confidence": 100,
      "version": "3.5.0",
      "icon": "Polymer.png",
      "website": "http://polymer-project.org",
      "cpe": null,
      "categories": [
        {
          "id": 12,
          "slug": "javascript-frameworks",
          "name": "JavaScript frameworks"
        }
      ],
      "rootPath": true
    },
    {
      "slug": "google-ads",
      "name": "Google Ads",
      "description": "Google Ads is an online advertising platform developed by Google.",
      "confidence": 100,
      "version": null,
      "icon": "Google Ads.svg",
      "website": "https://ads.google.com",
      "cpe": null,
      "categories": [
        {
          "id": 36,
          "slug": "advertising",
          "name": "Advertising"
        }
      ]
    },
    {
      "slug": "hammer-js",
      "name": "Hammer.js",
      "description": null,
      "confidence": 100,
      "version": "2.0.2",
      "icon": "Hammer.js.png",
      "website": "https://hammerjs.github.io",
      "cpe": null,
      "categories": [
        {
          "id": 59,
          "slug": "javascript-libraries",
          "name": "JavaScript libraries"
        }
      ],
      "rootPath": true
    },
    {
      "slug": "google-font-api",
      "name": "Google Font API",
      "description": "Google Font API is a web service that supports open-source font files that can be used on your web designs.",
      "confidence": 100,
      "version": null,
      "icon": "Google Font API.png",
      "website": "http://google.com/fonts",
      "cpe": null,
      "categories": [
        {
          "id": 17,
          "slug": "font-scripts",
          "name": "Font scripts"
        }
      ],
      "rootPath": true
    },
    {
      "slug": "recaptcha",
      "name": "reCAPTCHA",
      "description": "reCAPTCHA is a free service from Google that helps protect websites from spam and abuse.",
      "confidence": 100,
      "version": null,
      "icon": "reCAPTCHA.svg",
      "website": "https://www.google.com/recaptcha/",
      "cpe": null,
      "categories": [
        {
          "id": 16,
          "slug": "security",
          "name": "Security"
        }
      ]
    },
    {
      "slug": "google-ads-conversion-tracking",
      "name": "Google Ads Conversion Tracking",
      "description": "Google Ads Conversion Tracking is a free tool that shows you what happens after a customer interacts with your ads.",
      "confidence": 100,
      "version": null,
      "icon": "Google.svg",
      "website": "https://support.google.com/google-ads/answer/1722022",
      "cpe": null,
      "categories": [
        {
          "id": 10,
          "slug": "analytics",
          "name": "Analytics"
        }
      ]
    },
    {
      "slug": "hsts",
      "name": "HSTS",
      "description": "HTTP Strict Transport Security (HSTS) informs browsers that the site should only be accessed using HTTPS.",
      "confidence": 100,
      "version": null,
      "icon": "default.svg",
      "website": "https://www.rfc-editor.org/rfc/rfc6797#section-6.1",
      "cpe": null,
      "categories": [
        {
          "id": 16,
          "slug": "security",
          "name": "Security"
        }
      ],
      "rootPath": true
    },
    {
      "slug": "webpack",
      "name": "webpack",
      "description": "Webpack is an open-source JavaScript module bundler.",
      "confidence": 50,
      "version": null,
      "icon": "webpack.svg",
      "website": "https://webpack.js.org/",
      "cpe": null,
      "categories": [
        {
          "id": 19,
          "slug": "miscellaneous",
          "name": "Miscellaneous"
        }
      ]
    },
    {
      "slug": "pwa",
      "name": "PWA",
      "description": "Progressive Web Apps (PWAs) are web apps built and enhanced with modern APIs to deliver enhanced capabilities, reliability, and installability while reaching anyone, anywhere, on any device, all with a single codebase.",
      "confidence": 100,
      "version": null,
      "icon": "PWA.svg",
      "website": "https://web.dev/progressive-web-apps/",
      "cpe": null,
      "categories": [
        {
          "id": 19,
          "slug": "miscellaneous",
          "name": "Miscellaneous"
        }
      ],
      "rootPath": true
    },
    {
      "slug": "open-graph",
      "name": "Open Graph",
      "description": "Open Graph is a protocol that is used to integrate any web page into the social graph.",
      "confidence": 100,
      "version": null,
      "icon": "Open Graph.png",
      "website": "https://ogp.me",
      "cpe": null,
      "categories": [
        {
          "id": 19,
          "slug": "miscellaneous",
          "name": "Miscellaneous"
        }
      ],
      "rootPath": true
    },
    {
      "slug": "module-federation",
      "name": "Module Federation",
      "description": "Module Federation is a webpack technology for dynamically loading parts of other independently deployed builds.",
      "confidence": 50,
      "version": null,
      "icon": "Module Federation.png",
      "website": "https://webpack.js.org/concepts/module-federation/",
      "cpe": null,
      "categories": [
        {
          "id": 19,
          "slug": "miscellaneous",
          "name": "Miscellaneous"
        }
      ]
    },
    {
      "slug": "http-3",
      "name": "HTTP/3",
      "description": "HTTP/3 is the third major version of the Hypertext Transfer Protocol used to exchange information on the World Wide Web.",
      "confidence": 100,
      "version": null,
      "icon": "HTTP3.svg",
      "website": "https://httpwg.org/",
      "cpe": null,
      "categories": [
        {
          "id": 19,
          "slug": "miscellaneous",
          "name": "Miscellaneous"
        }
      ],
      "rootPath": true
    }
  ]
}

--我希望这样:

聚合物3.5.0

Hammer.js 2.0.2

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-16 18:53:20

@pmf解决方案的替代方案,对数据结构做出相同的假设:选择具有“真实”版本的对象:

代码语言:javascript
运行
复制
jq -r '.[] | select(.version) | "\(.name) \(.version)"' file.json

使用修正后的输入数据,您希望

代码语言:javascript
运行
复制
jq -r '.technologies[] | select(.version) | "\(.name) \(.version)"' file.json
# .....^^^^^^^^^^^^^^^

@pmf的解决方案也有相同的变化。

票数 0
EN

Stack Overflow用户

发布于 2022-11-16 18:15:31

使用替代算子 //,如果输入碰巧是错误的,则可以默认使用其他内容。使用empty 函数,您可以简单地丢弃该输入。

代码语言:javascript
运行
复制
jq -r '.[] | "\(.name) \(.version // empty)"'
代码语言:javascript
运行
复制
Polymer 3.5.0
Hammer.js 2.0.2

演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74465242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档