• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# JSON Lines
2
3The [JSON Lines](https://jsonlines.org) format is a text format of newline-delimited JSON. In particular:
4
51. The input must be UTF-8 encoded.
62. Every line must be a valid JSON value.
73. The line separator must be `\n`. As `\r` is silently ignored, `\r\n` is also supported.
84. The final character may be `\n`, but is not required to be one.
9
10!!! example "JSON Text example"
11
12    ```json
13    {"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]}
14    {"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]}
15    {"name": "May", "wins": []}
16    {"name": "Deloise", "wins": [["three of a kind", "5♣"]]}
17    ```
18
19JSON Lines input with more than one value is treated as invalid JSON by the [`parse`](../../api/basic_json/parse.md) or
20[`accept`](../../api/basic_json/accept.md) functions. To process it line by line, functions like
21[`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) can be used:
22
23!!! example "Example: Parse JSON Text input line by line"
24
25    The example below demonstrates how JSON Lines can be processed.
26
27    ```cpp
28    --8<-- "examples/json_lines.cpp"
29    ```
30
31    Output:
32
33    ```json
34    --8<-- "examples/json_lines.output"
35    ```
36
37!!! warning "Note"
38
39    Using [`operator>>`](../../api/operator_gtgt.md) like
40
41    ```cpp
42    json j;
43    while (input >> j)
44    {
45        std::cout << j << std::endl;
46    }
47    ```
48
49    with a JSON Lines input does not work, because the parser will try to parse one value after the last one.
50