Failed to create item: invalid JSON

Options
DFeiler
DFeiler
Community Member

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=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=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=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: jane.doe@acme.com
    password: A3Ztwsn4LWfFdBTuTwEh
    Test Field 1: my test secret
    URLs:
    : https://www.acme.com/login (primary)
    Additional Information: 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=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

Comments

  • 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="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.

  • bamiller3
    bamiller3
    Community Member
    Options

    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"

  • bamiller3
    bamiller3
    Community Member
    Options

    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.

  • 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

  • corevo
    corevo
    Community Member
    Options

    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.

  • 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

  • corevo
    corevo
    Community Member
    Options

    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

  • corevo
    corevo
    Community Member
    Options

    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.

  • 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

  • corevo
    corevo
    Community Member
    Options

    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

  • lntent
    lntent
    Community Member
    edited June 2022
    Options

    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

  • corevo
    corevo
    Community Member
    Options

    Hey, is there an update on this issue?

  • Hi @corevo, we are currently working on remediating this issue. Expect it to work properly in one of the next releases.

    Best,
    Andi

  • 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#v2060201

    Best,
    Horia

  • dawi
    dawi
    Community Member
    Options

    I am facing the same issue using op 2.8.0 via Java. :/

This discussion has been closed.