Merging nested JSON objects

If you have multiple nested JSON Objects (with no duplicate attribute names) like in this example object named Statement:

{
  "Amount": {
    "Currency": "EUR",
    "Value": "10.99"
    },
  "Valuation_Date": {
    "Date": "2022-01-24"
    },
  "References": {
        "FullRef": "CTC123456879",
        "ClearRef": "123456789"
        }
}

You can merge them into a flat one using the following code snipped:

{
  {% for object in [Statement.Amount, Statement.Valuation_Date, Statement.References] %}
    {% for key, value in object.items() %}
      "{{ key }}": "{{ value }}",
    {% endfor %}
  {% endfor %}
}

To receive:

{
    "Currency": "EUR",
    "Value": "10.99"
    "Date": "2022-01-24
    "FullRef": "CTC123456879",
    "ClearRef": "123456789"
}

Last updated