Keeping Order Numbers Exact

With the weights in grams and the money in cents, the port’s totals now match the Python script. Under each café’s invoice line, though, the script lists the corresponding orders by the web shop’s order numbers, so the café can tick them off against its own records.

Those numbers are nineteen digits long, and the café will search for them exactly as printed. In this lesson, you’ll port the lines that print them and make sure every digit survives.

The Python script reads the shop’s export with json.loads(), which turns whole JSON numbers into ints, and Python’s ints are exact at any size. Here’s the part that prints Larder Lane’s orders, with the export trimmed to that café’s three:

Language: Python
import json

EXPORT_TEXT = """{"total": "1303.20", "orders": [
    {"id": 1849327348870116187,
     "cafe": "Larder Lane", "kg": 12.6},
    {"id": 1851209668946987412,
     "cafe": "Larder Lane", "kg": 11.6},
    {"id": 1856614906249543762,
     "cafe": "Larder Lane", "kg": 11.8}
]}"""

shop_export = json.loads(EXPORT_TEXT)
for order in shop_export["orders"]:
    if order["cafe"] == "Larder Lane":
        print(f"  order {order['id']}: {order['kg']} kg")

In JavaScript, JSON.parse() does the job of json.loads(), turning JSON text into objects, arrays, strings, and numbers (JSON gets its own treatment later). A template literal can span lines like Python’s triple-quoted string, so the port reads like the Python version:

Language: JavaScript
const EXPORT_TEXT = `{"total": "1303.20", "orders": [
    {"id": 1849327348870116187,
     "cafe": "Larder Lane", "kg": 12.6},
    {"id": 1851209668946987412,
     "cafe": "Larder Lane", "kg": 11.6},
    {"id": 1856614906249543762,
     "cafe": "Larder Lane", "kg": 11.8}
]}`;

const shopExport = JSON.parse(EXPORT_TEXT);
for (const order of shopExport.orders) {
  if (order.cafe === "Larder Lane") {
    console.log(`  order ${order.id}: ${order.kg} kg`);
  }
}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.