-4

I have a ndjson file where each line describes an order:

Like this:

{"id": 6, "date": 15082021, "items": [{"id": 0, "name": "Smart TV", "price": 40}, {"id": 8, "name": "Smartphone", "price": 200}], "customer": {"id": 20, "name": "David Mexclar", "country": "Hungary"}}

I need to read the ndjson file and append each line in a list. So I can then easily do something like this:

orders[0]["customer"]["name"]

// output = David Mexclar

What is the best way to go about it please?

Thank you for any help!

lukas
  • 1
  • 2
  • The best way is to read [ask], then try *something* first, and show us your [mre]. – baduker Jun 01 '22 at 08:33
  • Your questions is somehow phony ... you're asking for list and at the same time your are giving this orders[0]["customer"]["name"] ... sorry, but your definitions aren't clear enough ... Please provide a minimal example of your result list – baskettaz Jun 01 '22 at 08:33
  • Hey @lukas please explain the `orders[0]` . – Rajan Jun 01 '22 at 08:41
  • they is asking for a list of dictionaries.... I don't think it is that difficult to understand. – alexpdev Jun 01 '22 at 08:44
  • There are a *lot* of duplicate questions. There's no `ndjson` format, that's just one of the names used to describe the technique of storing one JSON document per line. You'll find the same thing described as line-delimited JSON, JSON lines or streaming JSON. Someone tried to pass this common technique as their own invention, going as far as cloning the look-and-feel of json.org for their site and linking to W3C/ECMA JSON standards that have nothing to do with streaming JSON – Panagiotis Kanavos Jun 01 '22 at 08:47
  • Does this answer your question? [How I can I lazily read multiple JSON values from a file/stream in Python?](https://stackoverflow.com/questions/6886283/how-i-can-i-lazily-read-multiple-json-values-from-a-file-stream-in-python) – Panagiotis Kanavos Jun 01 '22 at 08:49
  • Another, more recent duplicate [Loading and parsing a JSON file with multiple JSON objects](https://stackoverflow.com/questions/12451431/loading-and-parsing-a-json-file-with-multiple-json-objects) - read the file line-by-line and parse each line. There are libraries like `jsonlines` and `json-stream` that do this as well – Panagiotis Kanavos Jun 01 '22 at 08:54

0 Answers0