op create item 'Secure Note' escaping problem

barnash
barnash
Community Member

Hi,

I'm trying to run the following command:
op create item 'Secure Note' $(echo '{"notesPlain":"","sections":[{"fields":[{"k":"concealed","t":"password","v":"\\c","n":"password"}],"title":"test","name":"test"}]}' | op encode) --title='test' --vault='MyVault'

It fails with:
[LOG] 2020/03/25 09:42:42 (ERROR) item data doesn't appear to be JSON or base64url encoded JSON: invalid character 'c' in string escape code

When I only run the echo command:
echo '{"notesPlain":"","sections":[{"fields":[{"k":"concealed","t":"password","v":"\\c","n":"password"}],"title":"test","name":"test"}]}'

I get the following:
{"notesPlain":"","sections":[{"fields":[{"k":"concealed","t":"password","v":"\c","n":"password"}],"title":"test","name":"test"}]}

Basically I'm trying to enter a field with the value of '\c' and not sure how to do it.
Can someone please help?

Thanks


1Password Version: 7.3.2
Extension Version: op version 0.9.2
OS Version: OSX 10.15.2
Sync Type: 1Password

Comments

  • cohix
    cohix
    1Password Alumni

    @barnash try running it through op encode first? echo "{your json"} | op encode

  • barnash
    barnash
    Community Member

    Not sure I got your comment. I think I solved this by double escaping the backslash. It's very strange and I think it might be an issue with op encode.

  • felix_1p
    felix_1p
    1Password Alumni
    edited March 2020

    Double encoding the backslash is indeed the solution to the problem but it's not an issue with op create item or op encode. The output of your echo command already shows that the generated JSON is invalid: The backslash is also the escape character in JSON string values, but JSON only allows specific escape sequences (see the diagram for string values in https://www.json.org/json-en.html), and \c is not one of them (that's what the error says, even though it could be clearer).

    In other words "\c" is not valid JSON. To produce the string value \c in JSON you have to generate JSON that looks like "\\c". But since \ is also used to as escape character in shells you end up with \\\\c.

    This seems to be a typical problem whenever you are using a language/syntax to produce input for another language/syntax and both use the same escape character.

This discussion has been closed.