It’s Cybersecurity Awareness Month! Join our interactive training session, or learn about security and AI from 1Password experts.
Forum Discussion
Former Member
4 years agoFailed 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
I am facing the same issue using op 2.8.0 via Java. :/
- Former Member
Hey y'all,
We just released CLI 2.6.2, which we expect to solve this issue. Let us know if that works for y'all!
https://app-updates.agilebits.com/product_history/CLI2#v2060201Best,
Horia - andi_t_1P
1Password Team
Hi @corevo, we are currently working on remediating this issue. Expect it to work properly in one of the next releases.
Best,
Andi - Former Member
Hey, is there an update on this issue?
- Former Member
Hey,
From what I understood it seemed like the 2.4 version should have fixed this issue but now it seems like the following command just hangs,
```
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}},
)
```seems like nothing returns from the command via stdout
- Former Member
Hey @"Horia.Culea_1P"
Can you advise on a workaroud for this script as well?
await execa(
'op',
['item', 'create', '--vault', vault, '--template', pathToItem, '--title', itemName],
)
Cheers,
Tomer - Former Member
Hey @corevo,
I was able to debug this with one of my colleagues who has encountered this problem themselves, using a JavaScript wrapper for the CLI (many thanks to @"jody.h_1P" for the help).
Indeed, there seems to be a bug that we have an internal ticket already tracking. In certain environments, it seems that the CLI interprets a programatic call as receiving piped input when it's not present, in fact.
We'll continue tracking this, and will update the thread when it is resolved. As a workaround, in the meantime, switching some of the input to be piped should help with the problem, before a fix is released:
```
import { execa } from 'execa'const result = await execa(
echo "${JSON.stringify({ title: "title" }).replace(/"/g, '\\"')}" | op
,
[
'item',
'create',
'-',
'--category=Password',
'--generate-password=letters,digits,symbols,32',
'--format=json'
],
{
shell: true
}
);
```Let us know if this helps.
Best,
Horia - Former Member
In addition make sure to run the repro using at least node 16 (due to ESM and top level await).
Don't forget to install execa from npm, and make sure the file ends with the
.mjs
extension so that node will interpret it as a native module. - Former Member
Hi,
Same as reported above the stderr is
exitCode: 1,
stdout: '',
stderr: '[ERROR] 2022/05/11 17:03:11 Failed to create item: invalid JSON',
I've updated from either 2.0 or 2.1, since the script requires the new V2 command line interface.
Cheers
- Former Member
Hey @corevo,
Thank you for reporting this.
Can you please let us know what error the command above causes, in your script?
Additionally, the version of 1Password CLI that you upgraded from would be useful.Best,
Horia