简体   繁体   中英

Azure Runbooks - Parse Python Params

I have a runbook and I am trying to basically parse parameters I entering into the body of the POST request (using Postman). I looked at this thread , but couldn't get it to work.

My runbook's code where I am trying to get the params:

mode = str(sys.argv[1])
resource_group_name = str(sys.argv[2])
vm_name = str(sys.argv[3])

here's my Postman call: 邮差

Error message:

in raw_decode obj, end = self.scan_once(s, idx)ValueError: Expecting property name: line 1 column 2 (char 1)

This is because when you pass the json from postman to runbook, the runbook will take the whole json string as one parameter, you can use print(sys.argv[1]) to check this behavior. The output like below:

在此处输入图片说明

In your case, there is a workaround. When you get the input parameter, get this section after RequestBody: , this one: {"resource_group_name":"vv1","vm_name":"vv2"},which is a json string Then you can parse the json string, get the value you want.

Sample code as below:

import sys
import json

#view the input parameter
print(sys.argv[1])

input_str = sys.argv[1]

# use "1" in index() method, to ignore the first { symbol in the input parameter
start_str = input_str.index("{",1)

end_str = input_str.index("}",1)

str = input_str[start_str:end_str+1]

text = json.loads(str)#parse the json string

#check the value
print("resource_group_name: "+text["resource_group_name"])
print("vm_name: "+text["vm_name"])

Test result as below:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM