sudhanshu saini
sudhanshu saini

Reputation: 51

how to read a json string as input to python script

i am writing a shell script test.sh inside which i am defining a json_str variable as

json_str='{"ecommerce": "master","app_compat":"master"}'

and i am passing this variable to a python script command - >

sudo python3 release.py $json_str

inside python script i am printing the value of input it is printing this

{"ecommerce":

not whole string. I can not change the input string as its coming from server which cant be changed. Solution of this is

json_str='{\"\ecommerce": \"\master",\"
\app_compat":\"\master"\}'

Can you suggest another method to do this as i cant change input string.

Inside python script

input_release=sys.argv[1]

print("here input %s" %input_release)

shell script

#!/usr/bin/env bash

echo "Inside bash script"
json_str='{"ecommerce": "master","app_compat":"master"}'

echo "$json_str"
sudo python3 release.py $json_str

Upvotes: 0

Views: 6231

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 148965

The problem is neither with Python nor with Json but with the shell itself. When you feed the shell with the following line:

sudo python3 release.py $json_str

here is what happens in that order:

  • the variable is replaced by its value giving sudo python3 release.py {"ecommerce": "master","app_compat":"master"
  • the line is tokenized in words: 1:sudo 2:python3 3:release.py 4:{"ecommerce": 5:"master","app_compat":"master" because of the space between : and "master"

The common way to avoid that splitting is to enclose the variable to be substituted in quotes. Unfortunately you cannot do it here because the string already contains quotes.

I can only imagine 2 solutions:

  1. ensure that the substitution string contains no unescaped space (one single \ because it occurs inside simple quotes):

    json_str='{"ecommerce":\ "master","app_compat":"master"}'
    
  2. use the standard input to read the string

    Python3: input_release = input() or Python2: input_release = rawinput()

    shell: echo $json_str | sudo python release.py, or with a here document:

    sudo python release.py <<END
    $json_str
    END
    

Upvotes: 0

zwer
zwer

Reputation: 25799

It breaks because of the spaces in your JSON. You can pass it as a single argument by quoting it:

sudo python3 release.py "$json_str"

But if you cannot alter the CLI you might try to recreate it within Python:

sudo python3 release.py $json_str

...

import sys

json_data = " ".join(sys.argv[1:])
print("JSON data: ", json_data)
# JSON data: {"ecommerce": "master","app_compat":"master"}

Although beware that you cannot account for all shell expansions this way and CLI is not really intended for passing large JSON-like structures so why don't you just pass it as an environment variable, i.e. in your shell script:

#!/usr/bin/env bash

json_str='{"ecommerce": "master","app_compat":"master"}'

export $json_str
sudo python3 release.py

and in your Python script:

import os

json_data = os.environ["json_str"]
print("JSON data: ", json_data)
# JSON data: {"ecommerce": "master","app_compat":"master"}

Upvotes: 2

Related Questions