如何通过AWS CLI JMESPath提取Step Functions执行历史JSON字符串字段
解决AWS Step Functions CLI查询执行输入字段返回空的问题
问题根源
你用aws stepfunctions get-execution-history拿到的executionStartedEventDetails.input是JSON格式的字符串,不是嵌套的JSON对象。直接用.comment或.inputs.name1这种JMESPath路径查询会返回空,因为字符串本身没有这些属性。
推荐解决方案(用jq处理)
借助jq解析字符串转JSON后提取字段,这是最可靠的方式:
aws stepfunctions get-execution-history --execution-arn <TestArn> --no-paginate --output json | jq '.events[] | .executionStartedEventDetails.input | fromjson | { comment: .comment, name1: .inputs.name1, name2: .inputs.name2, log_loc: (.inputs.params | split(" ") | .[1]) }'
这段命令的作用:
- 先用AWS CLI获取完整执行历史
- 通过
jq的fromjson把input字段的字符串转成JSON对象 - 提取你需要的
comment、name1、name2,并从params里拆分出-LOG_LOC对应的testPath值
纯AWS CLI方案(无jq,仅作参考)
如果必须只用AWS CLI的--query,可以通过字符串拆分提取,但兼容性差(遇到特殊字符会失效):
- 提取comment:
aws stepfunctions get-execution-history --execution-arn <TestArn> --no-paginate --output json --query 'events[*].executionStartedEventDetails.input | join(`""`, @) | split(`"comment": "`)[1] | split(`", "`)[0]'
- 提取name1:
aws stepfunctions get-execution-history --execution-arn <TestArn> --no-paginate --output json --query 'events[*].executionStartedEventDetails.input | join(`""`, @) | split(`"name1": "`)[1] | split(`", "`)[0]'
- 提取LOG_LOC值:
aws stepfunctions get-execution-history --execution-arn <TestArn> --no-paginate --output json --query 'events[*].executionStartedEventDetails.input | join(`""`, @) | split(`"params": "-LOG_LOC "`)[1] | split(`"`)[0]'
为什么之前的命令失效
你尝试的--query 'events[*].executionStartedEventDetails.input.comment'是在访问字符串类型的input的comment属性,而字符串没有这个属性,所以返回空结果。
内容的提问来源于stack exchange,提问作者SwapnilM
相关产品推荐
相关产品推荐

