skip to Main Content

Consider the following a snippet from my package.json

"dependencies": {
    "@types/lodash.camelcase": "^4.3.6",
    "@types/lodash.kebabcase": "^4.1.6",
    "@types/lodash.lowercase": "^4.3.6",
    "@types/lodash.upperfirst": "^4.3.6",
    "@types/mime": "^2.0.3",
    "@types/swagger-jsdoc": "^6.0.1",
    "async": "^3.2.2",
    "aws-arn": "^1.0.1",
}

From this, I want to generate the following string –

@types/lodash.camelcase@^4.3.6 @types/lodash.kebabcase@^4.1.6 @types/lodash.lowercase@^4.3.6 @types/lodash.upperfirst@^4.3.6 @types/mime@^2.0.3 @types/swagger-jsdoc@^6.0.1 async@^3.2.2 aws-arn@^1.0.1

Any idea how to accomplish this?

2

Answers


  1. As jq is tagged: Make a valid JSON, then disassemble with to_entries, and join twice:

    jq -r '.dependencies | to_entries | map(join("@")) | join(" ")' package.json
    
    @types/lodash.camelcase@^4.3.6 @types/lodash.kebabcase@^4.1.6 @types/lodash.lowercase@^4.3.6 @types/lodash.upperfirst@^4.3.6 @types/mime@^2.0.3 @types/swagger-jsdoc@^6.0.1 async@^3.2.2 aws-arn@^1.0.1
    

    Demo

    Login or Signup to reply.
  2. Assuming actually valid JSON, you can use string interpolation) and join:

    .dependencies | to_entries | map("(.key)@(.value)") | join(" ")
    

    String interpolation gives you a little more flexibility compared to using join twice. YMMV, and for sample cases join will suffice.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search