1

Is there a jmespath expression that will convert this:

{ "a": 5
, "b": [ {"c":6}, {"c":7}, {"c":8}]
}

into this:

[ {"a":5, "c":6}
, {"a":5, "c":7}
, {"a":5, "c":8}
]

Many thanks in advance!

Max Murphy
  • 1,701
  • 1
  • 19
  • 29
  • Note: I know that jmespath lacks the ability to reference parents, otherwise this could be implemented as `b[*].{a:../a,c:c}`. I don't see a cross product operator, with which I could get the result with: `cross(to_array(a),b).{a:[0],c:[1].c}`. Scanning through the docs I don't see anything else that might help. – Max Murphy Apr 06 '17 at 21:26

1 Answers1

1

Context

  • jmespath query
  • how to produce cross reference to parent object elements

Solution

  • This is possible with jmespath, but unfortunately the result has to be constructed manually, which obviously may not be what you want

Example

Given the following dataset ...

    { "a": 5
    , "b": [ {"c":6}, {"c":7}, {"c":8}]
    }

... the following jmespath query ...

    [
        @.{"a":"a","c":"b"[0].c}
        , @.{"a":"a","c":"b"[1].c}
        , @.{"a":"a","c":"b"[2].c}
    ]

... produces the following result

    [
      {"a":5, "c":6}
    , {"a":5, "c":7}
    , {"a":5, "c":8}
    ]

Pitfalls

  • Jmespath iterates over sequences quite brilliantly, but does not iterate over object keys (aka name-value pairs) with the same degree of flexibility
dreftymac
  • 31,404
  • 26
  • 119
  • 182