Skip to content

Instantly share code, notes, and snippets.

@simbalinux
Last active September 23, 2018 16:49
Show Gist options
  • Save simbalinux/4d67db7e5c9c886cfbf75cb3856ac97d to your computer and use it in GitHub Desktop.
Save simbalinux/4d67db7e5c9c886cfbf75cb3856ac97d to your computer and use it in GitHub Desktop.
parsing json using jq and bash arrays
#!/usr/bin/env bash
## use this to parse json and extract what you need and place in BASH as array with unique elements, if you need this to be a single "string" remove the \n in the print statement below.
echo '
{
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-plugin-lodash": "^3.3.4",
"eslint": "^5.4.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.8.0",
"jest": "^23.3.0",
"jest-junit-reporter": "^1.1.0",
"serverless-dynamodb-local": "^0.2.31",
"serverless-kms-secrets": "^1.0.3",
"serverless-offline": "^3.25.8",
"serverless-pseudo-parameters": "^2.1.0"
},
"dependencies": {
"@log4js-node/logstash-http": "^1.0.0",
"aws-sdk": "^2.277.1",
"axios": "^0.18.0",
"lodash": "^4.17.10",
"log4js": "^3.0.5",
"npm": "^6.4.0",
"object-hash": "^1.3.0"
}
}' > ./data.json
mapfile -t arr < <(jq -r '.devDependencies | keys[]' ./data.json)
printf "%s\n" ${arr[@]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment