Issue
I have a json fragment like below. I want to return the value of the name key together with the value of the version key if the version key has a value. The expected output is "name": "value" , "version" : "value"
It is better to have a solution with grep or jq.
node ~/wappalyzer/src/drivers/npm/cli.js https://youtube.com | jq .
Result:
{
"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
}
]
}
I expect this:
Polymer 3.5.0
Hammer.js 2.0.2
Solution
An alternate to @pmf's solution, making the same assumptions about the structure of the data: select the objects that have a "truthy" version:
jq -r '.[] | select(.version) | "\(.name) \(.version)"' file.json
With the corrected input data, you want
jq -r '.technologies[] | select(.version) | "\(.name) \(.version)"' file.json
# .....^^^^^^^^^^^^^^^
Same change for @pmf's solution.
Answered By - glenn jackman Answer Checked By - Mary Flores (WPSolving Volunteer)