Forum Discussion

Former Member's avatar
Former Member
4 years ago

Failed to create item: invalid JSON

Hi Team,
We've been using the 1Password CLI for quite some time, but are now trying to switch to CLIv2
In doing that, we've discovered some really odd behaviour, making it impossible for us to create new items.
Over the course of about 4 hours I've now managed to distill our script down to the smallest size to show the error we're experiencing.
The first call to op works, the second call gives us an error
What we're originally trying is parsing a CSV file with usernames & passwords and then adding those into our vault.
You will be able to see, that in my script I'm not even using the Username & Password variables I'm getting from my CSV file.
However, as soon as the script is inside a

while IFS=, read -r Username Password
do
....
done < ${CSVFile}

loop, we only get
[ERROR] 2022/04/22 14:42:37 Failed to create item: invalid JSON

from the script.

I will attach: The script, the csv file, the output.
Nevermind, I'm unable to attach the files, so I'll paste them in here

I am calling my script with the following parameters:

bash -x ./optest.sh test.csv

I'd be interested to see why this is happening.

================================

optest.sh

#!/bin/bash

CSVFile=$1

op item create --debug --category=login --title="Beginning" --vault="HARDCODEDVAULTIDFORTESTING" \
--url=https://www.acme.com/login --generate-password=20,letters,digits \
username=mailto:jane.doe@acme.com \
'Test Field 1=my test secret' --session "HARDCODEDSESSIONTOKENFORTESTING"

# Read from CSVFile, Splitting by "," separating into Variables
while IFS=, read -r Username Password; do
echo "Username: ${Username}"
echo "Password: ${Password}"
op item create --debug --title="WhileLoop" --category=login --vault="HARDCODEDVAULTIDFORTESTING" \
--url=https://www.acme.com/login --generate-password=20,letters,digits \
username=mailto:jane.doe@acme.com \
'Test Field 1=my test secret' --session "HARDCODEDSESSIONTOKENFORTESTING"
exit 0
done < ${CSVFile}

================================

test.csv

username,password

================================

script call & output

vagrant@ubuntu:~$ bash -x ./optest.sh test.csv

  • CSVFile=test.csv
  • op item create --debug --category=login --title=Beginning --vault=HARDCODEDVAULTIDFORTESTING --url=https://www.acme.com/login --generate-password=20,letters,digits username=mailto:jane.doe@acme.com 'Test Field 1=my test secret' --session HARDCODEDSESSIONTOKENFORTESTING ID: RANDOMID Title: Beginning Vault: HARDCODEDVAULTIDFORTESTING Created: now Updated: now Favorite: false Version: 0 Category: LOGIN Fields: username: mailto:jane.doe@acme.com password: A3Ztwsn4LWfFdBTuTwEh Test Field 1: my test secret URLs: : https://www.acme.com/login (primary) Additional Information: mailto:jane.doe@acme.com
  • IFS=,
  • read -r Username Password
  • echo 'Username: username' Username: username
  • echo 'Password: password' Password: password
  • op item create --debug --title=WhileLoop --category=login --vault=HARDCODEDVAULTIDFORTESTING --url=https://www.acme.com/login --generate-password=20,letters,digits username=mailto:jane.doe@acme.com 'Test Field 1=my test secret' --session HARDCODEDSESSIONTOKENFORTESTING [ERROR] 2022/04/22 14:51:59 Failed to create item: invalid JSON
  • exit 0

================================

Thanks for taking a look.

Cheers,
Dominik


1Password Version: 2.0.2
Extension Version: Not Provided
OS Version: Ubuntu 20.04

15 Replies

  • Former Member's avatar
    Former Member

    Hi, we are facing the same issue since the latest update (2.2.0), we are using 1password CLI as part of a CI script in javascript

    Repro attached
    ```
    import {execa} from 'execa'

    const res = await execa(
    'op',
    [
    'item',
    'create',
    '--category',
    'Password',
    '--title',
    title,
    '--generate-password=letters,digits,symbols,32',
    '--format',
    'json',
    ],
    {env: {OP_SESSION_org: sessionId}},
    )

    ```

    The command works if I copy and paste it to a terminal window.

  • Former Member's avatar
    Former Member

    Hey bamiller3, sorry for the late response!
    I am glad you have figured out a solution for the problem described above.
    Please do let us know if there is anything else that we could help with!

    Best,
    Horia

  • bamiller3's avatar
    bamiller3
    New Contributor

    Just an update on this. I found that I was able to schedule the job through Windows task scheduler and it worked. I was previously using JAMS scheduler for this. I've never ran into any issues like this before using JAMS. Thanks.

  • bamiller3's avatar
    bamiller3
    New Contributor

    I am having a similar issue with CLIv2. When executing the following at a Powershell prompt, it works fine but when run it via a scheduled job, I get the "Failed to create item: invalid JSON" message. I have not tried using v1 and I'd rather not use an old version if I don't have to.

    op item create --category="login" --title="$title" --vault="$vault" username="$user" password="$password"

  • Former Member's avatar
    Former Member

    Hey @DFeiler ,

    Thank you for reaching out to us about your script.

    Would it be possible to give it a try after wrapping all flag values in double quotes?

    For example (taken from your example):

    op item create --debug --title="WhileLoop" --category="login" --vault="HARDCODEDVAULTIDFORTESTING" --url="https://www.acme.com/login" --generate-password="20,letters,digits" username="mailto:jane.doe@acme.com" 'Test Field 1=my test secret' --session "HARDCODEDSESSIONTOKENFORTESTING"

    I will continue to investigate your issue, but please let me know if this ends up working for you.