3

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???

5
  • passing the json is clumsy .. cant you just pass the file name? Commented Nov 2, 2020 at 15:28
  • I hope you're not sharing your actual password there... Commented Nov 2, 2020 at 15:29
  • Searching with variations of python pass a string as a command line argument site:stackoverflow.com should get you on your way. Commented Nov 2, 2020 at 15:31
  • @wwii thanks for the comment. Definitely I'll refer Commented Nov 2, 2020 at 16:17
  • @balderman it's the requirement to pass like that Commented Nov 2, 2020 at 16:18

1 Answer 1

8

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"}​​]}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.