How to take key value pairs and turn them into an array of objects using JMESPath How to take key value pairs and turn them into an array of objects using JMESPath json json

How to take key value pairs and turn them into an array of objects using JMESPath


Context

  • jmespath query language
  • how to re-normalize data from one structure (schema) to another
  • how to effectively iterate and filter on object properties (name-value pairs)

Pitfalls

  • Generally speaking, jmespath is highly flexible in iterating sequentially-indexed arrays, but much less flexible in iterating over object properties
  • Most transformations with jmespath become extremely cumbersome when the goal is to iterate over object properties
  • Usually you can produce any arbitrary output in jmespath if you are willing to "manually construct" the output by hand ... this is usually the trade-off when dealing with iterating over object properties (aka name-value pairs)

Example

Given the following original dataset ...

{"time": 32,    "terms": {        "192.168.10.121": 84,        "154.223.10.121": 12,        "112.149.10.121": 6    }}

... the following jmespath query ...

{"data": [  { "ip_address": @.terms|keys(@)[0], "count": @.terms|values(@)[0] }  ,{ "ip_address": @.terms|keys(@)[1], "count": @.terms|values(@)[1] }  ,{ "ip_address": @.terms|keys(@)[2], "count": @.terms|values(@)[2] }]}

... produces the following result

{"data": [    { "ip_address": "192.168.10.121", "count": 84 },    { "ip_address": "154.223.10.121", "count": 12 },    { "ip_address": "112.149.10.121", "count": 6 }]}