Back to Practice
#0074

Total Values From JSON Records

MediumPython20 min15 XP

Problem

Given a JSON string representing a list of records and a numeric key, return the sum of that key across records. Missing keys count as 0.

Why This Matters

APIs, logs, configs, and notebooks often exchange JSON. You need to parse first, then reason over ordinary Python dictionaries.

Function Signature

def total_json_key(text, key):

Examples

Example 1
Input'[{"amount": 10}, {"amount": 5}, {"name": "x"}]', "amount"
Output15

The third record does not have amount, so it contributes 0.

Constraints

  • Input JSON is valid.
  • The top-level value is a list of objects.
  • Missing keys count as zero.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Missing key contributes zero
InputJSON list and key amount
Expected15

Only records with amount add to the total.

Hidden Test Categories
empty listnegative valuesdifferent key name