简体   繁体   English

优雅的方式来获得价值Boto3响应字典

[英]Elegant way to get a value out Boto3 response dict

I'm using boto3 to describe my pipelines to me. 我正在使用boto3向我描述我的管道。 It throws back a response in a very curious format - 它以非常好奇的形式回复了一个回复 -

{'ResponseMetadata': {'HTTPStatusCode': 200,
                      'RequestId': '2c42a320-c6ab-11e5-a021-afa8e3e7ca24'},
 u'pipelineDescriptionList': [{u'fields': [{u'key': u'key1', u'stringValue': u'AIDAIxxxxxxNWNI6I'},
                                           {u'key': u'key2', u'stringValue': u'2016-01-29T13:32:09'},
                                           {u'key': u'key3', u'stringValue': u'ABCD'},
                                           {u'key': u'key4', u'stringValue': u'PIPELINE'}],
                               u'name': u'ABCD',
                               u'pipelineId': u'df-03442XXXXAZTRKE4NNK',
                               u'tags': []},
                              {u'fields': [{u'key': u'key1',u'stringValue': u'2016-01-29T11:26:02'},
                                           {u'key': u'key2', u'stringValue': u'[{"key":"jobrun","value":"RzIN"},{"key":"env","value":"prod"}]'},
                                           {u'key': u'key3', u'stringValue': u'2016-01-29T11:25:20'},
                                           {u'key': u'key4', u'stringValue': u'PIPELINE'}],
                               u'name': u'test-pipeline',
                               u'pipelineId': u'df-006474936ZXRQ238Q70O',
                               u'tags': [{u'key': u'jobrun', u'value': u'RWssN'},{u'key': u'env', u'value': u'prod'}]}]}

Now I'm interested in getting pipeline 'ABCD's key2 value. 现在我有兴趣获得管道'ABCD的key2值。 If I try to get it in the most intuitive way, it gets very messy since I'll have to iterate through each of the pipelineDescriptionList list items, iterate through each of the list items in 'fields', check the one for which key equals key2 , and get the stringValue for that. 如果我试图以最直观的方式获取它,它会变得非常混乱,因为我将不得不遍历每个pipelineDescriptionList列表项,遍历'fields'中的每个列表项,检查哪个key等于key2 ,并获取stringValue

My question is, are we actually expected to deal with this mess? 我的问题是,我们实际上是否期望处理这个烂摊子? Or is there some Python magic that can make my task more elegant? 或者是否有一些Python魔法可以让我的任务更优雅?

One option would be to use jmespath which is bundled with boto3. 一种选择是使用jmespath其捆绑boto3。 It's a pretty powerful tool for processing JSON-like data structures. 它是一个非常强大的工具,用于处理类似JSON的数据结构。

For example, if the response you show above was bound to the variable response you could do this: 例如,如果您在上面显示的响应绑定到变量response ,则可以执行以下操作:

jmespath.search("pipelineDescriptionList[?name == 'ABCD'].fields[] | [?key == 'key2'].stringValue", response)

which would return: 将返回:

[u'2016-01-29T13:32:09']

There may be better ways to do it, that's just a quick cut at it. 可能有更好的方法来做到这一点,这只是快速切入它。 You can also compile these expressions to make things a bit more efficient for doing many searches with the same query. 您还可以编译这些表达式,以便使用相同的查询进行多次搜索,从而提高效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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