Incorrect queries in docs?

I'm copy-pasting a query from this page: Scripted metric aggregation | Elasticsearch Guide [7.17] | Elastic

the query is

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "profit": {
      "scripted_metric": {
        "init_script": "state.transactions = []", 
        "map_script": "state.transactions.add(doc.type.value == 'sale' ? doc.amount.value : -1 * doc.amount.value)",
        "combine_script": "double profit = 0; for (t in state.transactions) { profit += t } return profit",
        "reduce_script": "double profit = 0; for (a in states) { profit += a } return profit"
      }
    }
  }
}

However, Kibana is throwing an error: "Request error: parsing_exception, [match_all] malformed query, expected [END_OBJECT] but found [FIELD_NAME]".
I understand that the query will not work on my cluster because of the field names, but this seems to be an issue with the format, and I'm not really why I'm getting this error? I tried different things and sometimes I get an error that "aggs" is an unknown field.