Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass json string as a command line argument using python

I have written a code to parse and get the values.This is the code:


    
with open("config.json","r")as infile:
     inputs = json.load(infile)
for item in range(len(inputs["config"])):
     IP = inputs["config"][item]["IP"]
     PORT = inputs["config"][item]["PORT"]
     USERNAME = inputs["config"][item]["USERNAME"]
     PASSWORD = inputs["config"][item]["PASSWORD"]
     ENABLE = inputs["config"][item]["ENABLE"]
     if ENABLE == "True":
         

But, instead of opening a file like this, I would like to pass the json_string as a command line argument so it can read from command line and get the values in json_string to the variables. I have a json_string like this:


    
{​​
"cameras": [
{​​
"ipAddress": "10.27.25.164",
"url": "rtsp://admin:[email protected]/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif",
"userName": "admin",
"userPasswd": "Aviro",
"port": "80"
}​​
]
}​​

I would like to know, how to pass json_string as a command line argument and what are the changes i need to modify in the code, so the values in the json_string assigned to the given variables.

can anyone help me with this???

like image 283
Sai Avatar asked Mar 06 '26 03:03

Sai


1 Answers

Yes read from command line arguments like this:

import sys,json
inputs = json.loads(sys.argv[1])

And pass it like:

SCRIPT.py '{​​"cameras": [{​​"ipAddress": "10.27.25.164","url": "rtsp://admin:[email protected]/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif","userName": "admin","userPasswd": "Aviro","port": "80"}​​]}'
like image 59
programmer365 Avatar answered Mar 07 '26 17:03

programmer365



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!