json

FormatValidateConvert

In this section · Start from two files or a questionCompare, find, extract

Start from two files or a question

What changed between two files, and pull it out

Two days of stock side by side, one query run in each pane, and the low items taken away as a document.

Guide 3 of 7 in Recipes

A warehouse sends its stock levels as a JSON export once a day, and you have yesterday’s and today’s. Two questions follow, always in the same order. What moved overnight? And of today’s items, which ones need reordering — as a list you can send on, not a screenshot? The first is a comparison, the second a query, and the answer to the second is a small document in its own right. All three steps happen in the editor’s Compare mode, on the same two files, without either one leaving the page.

1 · See what changed

Compare keeps a document in each pane and marks what differs. Monday goes on the left, because the summary reads from left to right: what the right pane added, removed and changed relative to the left.

Left pane

stock-monday.json

{
  "warehouse": "Leeds",
  "items": [
    { "sku": "TEA-12", "stock": 40 },
    { "sku": "CUP-04", "stock": 12 },
    { "sku": "POT-01", "stock": 6 }
  ]
}

Right pane

stock-tuesday.json

{
  "warehouse": "Leeds",
  "items": [
    { "sku": "TEA-12", "stock": 38 },
    { "sku": "CUP-04", "stock": 0 },
    { "sku": "POT-01", "stock": 6 },
    { "sku": "KET-02", "stock": 0 }
  ]
}

Do

  1. In the editor band, under Mode, choose Compare.
  2. Paste Monday’s file into the left pane and Tuesday’s into the right.
  3. Read the summary in the status bar, then press Next difference to walk through them.

Result

+1 −0 ~2

changed  $.items[0].stock
changed  $.items[1].stock
added    $.items[3]

Try it in the editor →

One item added and two changed, nothing removed. The list under the summary is the order Next difference visits them in, which follows the left document from top to bottom, with the new kettle last because it exists only on the right. Tea dropped by two and cups ran out entirely; the teapots did not move, so they are not listed at all.

The comparison is by structure, not by line. Reordering the keys inside an item would not count as a change, and neither would reformatting one file with four-space indents. Only values that differ, and keys or items that exist on one side alone, are counted.

2 · Ask the same question of both days

The comparison says what moved, but not what matters. A query does: every item with fewer than ten in stock. The block carries on with the same two files, and runs the query once in each pane, because each pane in Compare has a Find bar of its own.

Left pane

stock-monday.json

{
  "warehouse": "Leeds",
  "items": [
    { "sku": "TEA-12", "stock": 40 },
    { "sku": "CUP-04", "stock": 12 },
    { "sku": "POT-01", "stock": 6 }
  ]
}

Right pane

stock-tuesday.json

{
  "warehouse": "Leeds",
  "items": [
    { "sku": "TEA-12", "stock": 38 },
    { "sku": "CUP-04", "stock": 0 },
    { "sku": "POT-01", "stock": 6 },
    { "sku": "KET-02", "stock": 0 }
  ]
}

Do

  1. Press ⌘F on a Mac or Ctrl+F on Windows and Linux, with the caret in the left pane.
  2. Press Query mode ({ }) on the bar, and type $.items[?@.stock < 10].
  3. Click into the right pane, press ⌘F or Ctrl+F again, press Query mode on that bar too and type the same query.

Result

Left: 1/1
/items/2

Right: 1/3
/items/1
/items/2
/items/3

Try it in the editor →

On Monday one item was low, the teapots at index 2. On Tuesday three are: the cups, the teapots again, and the kettle that did not exist the day before. The counter is per pane, and so are the marks, which is what makes Compare the right place for this: you are asking one question of two documents, and each answers for itself.

Try it under the block opens both files in Compare with the query already typed in the left pane’s bar. The right pane’s bar starts empty; put the caret in the right pane, press the shortcut again, press Query mode on that bar and type the same query. The two bars do not share a search on purpose, since the usual reason to be in Compare is that the two documents need different questions.

3 · Take Tuesday’s answer away

The three low items are what you would send to whoever reorders. Extract turns the matches in one pane into a document in the other, so the step starts from the right pane, whose search is still showing.

Left pane

stock-monday.json

{
  "warehouse": "Leeds",
  "items": [
    { "sku": "TEA-12", "stock": 40 },
    { "sku": "CUP-04", "stock": 12 },
    { "sku": "POT-01", "stock": 6 }
  ]
}

Right pane

stock-tuesday.json

{
  "warehouse": "Leeds",
  "items": [
    { "sku": "TEA-12", "stock": 38 },
    { "sku": "CUP-04", "stock": 0 },
    { "sku": "POT-01", "stock": 6 },
    { "sku": "KET-02", "stock": 0 }
  ]
}

Do

  1. In the right pane’s Find bar, with the query still typed, press Extract matches into the other pane.
  2. The editor asks before it replaces Monday’s file; press Replace anyway.
  3. Read the left pane, which now holds the extracted document.

Result

{
  "items": [
    {
      "sku": "CUP-04",
      "stock": 0
    },
    {
      "sku": "POT-01",
      "stock": 6
    },
    {
      "sku": "KET-02",
      "stock": 0
    }
  ]
}

Try it in the editor →

The extracted document keeps the shape around the matches — an items array — and drops everything else, including the warehouse name, because no match was inside it. Each item comes out whole, so its SKU travels with its count. The status bar says how many matches went into which pane.

Extracting from the right pane writes into the left one, which means Monday’s file is replaced, so the editor asks first. That is the natural end of this job, since you have finished comparing, and it is one undo: ⌘Z or Ctrl+Z in the left pane brings Monday back if you want another look. From there the new document is an ordinary one — download it, copy it, or take it to Convert to send it on as CSV.

Variations

The query is the part to change. Swap the filter for $.items[?@.stock == 0] to see only what has run out, or use a plain text search for a single SKU to see its line in both files at once. If the files are large and the differences few, walk them with Next difference first; the gutter marks each one, and where the changes cluster is often the query you want to write next.

If you only need the extracted items and not the comparison, you can skip Compare altogether: paste Tuesday’s file alone, run the query in Mirror, and press Extract. The editor moves to Compare for you, because Extract always needs a second pane to write into.

Where to go from here

Open the editor and switch Mode to Compare to begin.