我正在获取Service Nowsctask
#。我能够调用API并获得所需的sctasks和描述,但由于字符串管理不好,很难解析出来。
输出
msg": {
"result": [
{
"active": "true",
"activity_due": "",
"additional_assignee_list": "",
"approval": "not requested",
"approval_history": "",
"approval_set": "",
"assigned_to": {
"link": "url",
"value": "value"
},
"assignment_group": {
"link": "group name",
"value": "value"
},
"number": "SCTASK0000007",
代码
- name: GET SCTASKS
ignore_errors: yes
uri:
url: https://abc.service-now.com/api/now/table/sc_task?sysparm_query=assignment_group.name=mygroup^active=1
user: username
password: password
method: GET
body_format: json
register: output
- set_fact:
tasknum: "{{ output.json | json_query(query) }}"
vars:
query: "[].[result].[number].value"
- name: Print all Task numbers
debug:
msg: "{{ item}}"
with_items:
- "{{ tasknum }}"
如有任何帮助,不胜感激。谢谢
创建一个属性的数字值列表。列表中只有一项结果
result:
- active: 'true'
activity_due: ''
additional_assignee_list: ''
approval: not requested
approval_history: ''
approval_set: ''
assigned_to:
link: url
value: value
assignment_group:
link: group name
value: value
number: SCTASK0000007
因此,下面的任务
- debug:
msg: "{{ result|json_query(_query) }}"
vars:
_query: "[].{scnumber: number}"
给出一个列表,其中一个项目太
msg:
- scnumber: SCTASK0000007
从字典中只选择了一个属性。只创建的值列表会更简单
- set_fact:
scnumbers: "{{ result|json_query(_query) }}"
vars:
_query: "[].number"
给出
scnumbers:
- SCTASK0000007
然后,映射属性也会创建列表
- set_fact:
scnumbers: "{{ result|map(attribute='number')|list }}"
问题通过以下代码解决。
- name: print tasknum
debug: msg="{{ jsoncontent.json | json_query(query) }}"
vars:
query: "result[*].{scnumber: number}"