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

Question: how to produce multiline strings? #190

Closed
datspike opened this issue Oct 12, 2022 · 2 comments
Closed

Question: how to produce multiline strings? #190

datspike opened this issue Oct 12, 2022 · 2 comments

Comments

@datspike
Copy link

Newline character seem to always get escaped to \\n

spike@aelesinpc ~> jo -p chat_id=$TGCHAT_ID text="$CI_COMMIT_AUTHOR \n $CI_COMMIT_BRANCH"
{
   "text": " \\n "
}
spike@aelesinpc ~> jo -p chat_id=$TGCHAT_ID text="$CI_COMMIT_AUTHOR \\n $CI_COMMIT_BRANCH"
{
   "text": " \\n "
}
@datspike datspike changed the title Question: how to product multiline strings? Question: how to produce multiline strings? Oct 12, 2022
@gromgit
Copy link
Collaborator

gromgit commented Oct 12, 2022

Bare newlines are not permitted in JSON strings. See the string grammar in https://www.json.org/json-en.html.

That said, you actually encoded a literal backslash followed by a literal n. Here's how to encode a newline in a JSON string with jo:

$ CI_COMMIT_AUTHOR=aho CI_COMMIT_BRANCH=main

# actual newline
$ jo -p text="$CI_COMMIT_AUTHOR 
 $CI_COMMIT_BRANCH"
{
   "text": "aho \n main"
}

# bash-generated newline
$ jo -p text="$CI_COMMIT_AUTHOR "$'\n'" $CI_COMMIT_BRANCH"
{
   "text": "aho \n main"
}

@datspike
Copy link
Author

@gromgit thanks a lot.

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

2 participants