Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to process key with colon and other characters? #154

Open
dracorp opened this issue Aug 12, 2021 · 3 comments
Open

How to process key with colon and other characters? #154

dracorp opened this issue Aug 12, 2021 · 3 comments

Comments

@dracorp
Copy link

dracorp commented Aug 12, 2021

I have an issue:

echo '{"scripts":{"build:apps":"build_apps.sh"}}' | json
{
  "scripts": {
    "build:apps": "build_apps.sh"
  }
}

Now, I want modify this value:

echo '{"scripts":{"build:apps":"build_apps.sh"}}' | json -e 'this.build:apps="foo"'
undefined:3
this.build:apps="foo"
          ^

SyntaxError: Unexpected token ':'
    at new Function (<anonymous>)
    at main (/Users/u537501/Projects/Projects-lsy/CrewApps/AzureDevOps/CrewPortal/src/LayoverApp/node_modules/json/lib/json.js:1365:27)
    at Object.<anonymous> (/Users/u537501/Projects/Projects-lsy/CrewApps/AzureDevOps/CrewPortal/src/LayoverApp/node_modules/json/lib/json.js:1764:5)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47

How to handle colon? I've tried with backslash, double/single quotes.

The same situation is with minus:

echo '{"scripts":{"build-apps":"build_apps.sh"}}' | json -e 'this.build-apps="foo"'
undefined:3
this.build-apps="foo"
^^^^^^^^^^^^^^^

SyntaxError: Invalid left-hand side in assignment

or

echo '{"scripts":{"build-apps":"build_apps.sh"}}' | json -e 'this."build-apps"="foo"'
undefined:3
this."build-apps"="foo"
     ^^^^^^^^^^^^

SyntaxError: Unexpected string

The jq program parses those characters:

$ echo '{"scripts":{"build-apps":"build_apps.sh"}}' | jq '.scripts."build-apps"'
"build_apps.sh"
@mirkonz
Copy link

mirkonz commented Oct 28, 2021

json -e 'this["build:apps"]="foo"'
should do the trick

@dracorp
Copy link
Author

dracorp commented Oct 28, 2021

Nice trick, thank you. Where did the idea for this syntax come from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants