SmartQueryTools

Get Top N Rows from JSON Files Online

Extract the top N rows per group from JSON files directly in your browser. Get the top 5 products per category, highest scores per team, or any ranked subset — no upload required.

How to get Top N Rows from JSON files

  1. Drop your file onto the upload area. The first 200 rows are shown and the first numeric column is pre-selected as the ranking column.
  2. Choose Rank by column and a Direction: Highest first for the largest values, Lowest first for the smallest.
  3. Optionally choose a Group by column to get the top rows within each group. Leave it on (none) to rank the whole table.
  4. Set Top N (default 5). Tick Include rank position column if you want a rank_in_group column showing 1, 2, 3 within each group.
  5. Click Get Top N, check the row count and preview, then download the result in the same format.

Your file is processed locally in your browser and is never uploaded. The free limit is 50 MB per file; larger files work if your device has the memory for them.

Worked example

A race organiser has marathon finish times and wants the two fastest runners in each age group for the podium list.

Input (JSON)

[
  {
    "runner": "Aroha Ngata",
    "age_group": "F40-49",
    "finish_minutes": 212.5
  },
  {
    "runner": "Megan Doyle",
    "age_group": "F40-49",
    "finish_minutes": 198.25
  },
  {
    "runner": "Lena Fischer",
    "age_group": "F40-49",
    "finish_minutes": 205
  },
  {
    "runner": "Tom Reid",
    "age_group": "M50-59",
    "finish_minutes": 187.75
  },
  {
    "runner": "Raj Patel",
    "age_group": "M50-59",
    "finish_minutes": 201
  },
  {
    "runner": "Owen Clarke",
    "age_group": "M50-59",
    "finish_minutes": 179.5
  }
]

Settings

  • Rank by column: finish_minutes
  • Direction: Lowest first
  • Group by: age_group
  • Top N: 2
  • Include rank position column: on

Result

runnerage_groupfinish_minutesrank_in_group
Megan DoyleF40-49198.251
Lena FischerF40-492052
Owen ClarkeM50-59179.51
Tom ReidM50-59187.752

Lower times are better, so Lowest first is used. Each age group is ranked on its own, and the slowest runner in each group drops out. The output is sorted by age_group and then by finish_minutes. The rank_in_group column is added at the end only because the rank option was ticked. With Group by left on (none), the result would be the two fastest runners overall: Owen Clarke and Tom Reid.

Working with JSON files

In a JSON array of objects, a key that always holds numbers loads as a numeric column and ranks numerically. If some objects store the value as a string, for example "score": "88" next to "score": 91, the column loads with a mixed JSON type and ranks as text. Keep numbers unquoted in the source to get a true numeric ranking. The column list shows the loaded type next to each key, so you can check before running.

Objects that omit the ranking key get NULL and are sorted after every real value. Objects that omit the group key are collected into one NULL group, which gets its own top N. Nested objects cannot be used directly for grouping on an inner field, so flatten the JSON first if you want to group by something like store.region. The output is a JSON array sorted by group and rank. rank_in_group is added as the last key of each object when the option is on.

Frequently Asked Questions

What happens to JSON objects that are missing the group key?

They are grouped together under NULL and ranked as one group, so up to N of them appear in the output.

Can I group by a nested JSON field?

Not directly. Flatten the file so the nested field becomes a top-level column, then choose it in Group by.

What happens when two rows tie at the Nth position?

The tool uses ROW_NUMBER, so exactly N rows are returned per group and one of the tied rows is dropped. Which one is not guaranteed. If ties matter, use the Rank tool, which supports RANK and DENSE_RANK, and then filter.

Can I rank by a text or date column?

Yes. Rank by lists every column. Text ranks alphabetically and dates rank chronologically. The first numeric column is only the default.

Where are NULL values placed in the ranking?

After all non-NULL values, whichever direction you choose. They only appear in the result when a group has fewer than N rows with a value.

Related Tools