input = json.load(sys.stdin)
print(input['id'])
When I input {"id":1} and hit enter, my program does not continue, I am just stuck typing in my input. How can I make the program continue after valid json has been passed to my stdlin?
when you read in from sys.stdin it will read everything until it hits an EOF character normally ctrl-d so if you input {"id":1} <ENTER> ctrl-d it should work.
It looks like what you are trying to do is something like this
import json
json_as_str = input()
json_obj = json.loads(json_as_str)
print(json_obj['id'])
I tried the code interactively by pasting the two lines in and then realised the second line was being interpreted as part of the JSON the first line was reading! As a module the code works perfectly predictably. The visible input was followed by a newline and CTRL/D on the console.
sholden$ cat /tmp/py.py
import json, sys
input = json.load(sys.stdin)
print(input['id'])
sholden$ python /tmp/py.py
{"id": 42}
42
importstatements in your code. And don't useinputas a variable name, that shadows the built-ininputfunction.json.loadtakes an open file and reads its whole contents, and the documentation states that it usesread()to do so.