Debian Codegraph @

texmf-dist/doc/latex/jsonparse/README.md

raw
1 ![Version 1.7.2](https://img.shields.io/badge/version-1.7.2-blue)
2
3 ![Jason, the JSON parsing horse](https://github.com/jasperhabicht/jsonparse/assets/6378801/ddfddc70-bf5f-4121-ba45-4b9128875d85)
4
5 # The `jsonparse` package
6
7 The `jsonparse` package provides a handy way to read in JSON data from files
8 or strings in LaTeX documents, parse the data and store it in a user-defined
9 token variable. The package allows accessing the stored data via a
10 JavaScript-flavored syntax.
11
12 Using the commands `\JSONParseFromFile` or `\JSONParse`, JSON data can be
13 stored in a token variable. Using the command `\JSONParseValue`, entries can
14 be extracted from the stored data.
15
16 ---
17
18 Let's assume a file with the name `example.json` is stored in the working
19 directory with the following contents:
20
21 ```json
22 {
23 "givenName": "Joe",
24 "familyName": "Public",
25 "points": 1.7,
26 "hasCertification": true,
27 "address": {
28 "streetAddress": "10 Main St",
29 "locality": "Cityville",
30 "postalCode": "12345"
31 },
32 "contactPoint": [
33 {
34 "contactType": "office",
35 "telephone": "+1 (555) 555-1234"
36 },
37 {
38 "contactType": "mobile",
39 "telephone": "+1 (555) 555-6789"
40 }
41 ],
42 "children": [
43 "Tom",
44 "Doug",
45 "Harry"
46 ],
47 "memberOf": null
48 }
49 ```
50
51 We can store it in the token variable `\myJSONdata` using the command
52 `\JSONParseFromFile{\myJSONdata}{example.json}`. Calling the command
53 `\JSONParseValue{\myJSONdata}{contactPoint[0].telephone}` would then result in
54 the output `+1 (555) 555-1234` (indices are zero-based per default). The
55 package allows for parsing JSON data inline as well.
56
57 The package also offers several commands for looping through arrays and
58 accessing individual elements, for example to typeset them in tabular form or
59 to plot their values using packages such as PGFPlots.
60
61 Adding commas between the items of the children array can be done with:
62 ```tex
63 \JSONParseArrayUse{\myJSONdata}{children}{, }
64 ```
65
66 A tabular listing the contact points of above JSON file can be created with:
67 ```tex
68 \begin{tabular}{cc}
69 \textbf{Contact Type} & \textbf{Telephone} \\
70 \JSONParseArrayMapInline{\myJSONdata}{contactPoint}{%
71 \JSONParseValue{\myJSONdata}{contactPoint[#1].contactType} &
72 \JSONParseValue{\myJSONdata}{contactPoint[#1].telephone} \\
73 }
74 \end{tabular}
75 ```
76
77 The package also provides a few helper commands such as to validate a JSON
78 number or to convert Unicode surrogate pairs to the relevant Unicode codepoint.
79
80 ---
81
82 **Frequently asked questions**
83
84 *Why does a comparison of two equal strings return false?*
85
86 1) Note that you probably need to use `\JSONParseExpandableValue` if you want
87 to compare a value from parsed JSON data with the contents of another token
88 list.
89
90 2) Also note that `\JSONParseExpandableValue` returns a string with specific
91 category codes, namely category code 12 ("other") for all characters except
92 spaces. Therefore, you need to test against another string. You can use
93 `\detokenize` for conversion.
94
95 *Why does parsing take so long?*
96
97 1) Make sure that you use the most recent version of the package. Parsing speed
98 is constantly monitored and updates typically increase parsing speed.
99
100 2) If you don't need to access whole objects or arrays from the JSON data, you
101 can set the key `skip structures` which should increase parsing speed.
102
103 3) If the JSON data contains a considerable amount of numbers, parsing speed can
104 be increased slightly by setting `validate numbers=false`.
105
106 4) If the JSON data does not change often, you can externalise the parsed result
107 by setting the key `externalize`.
108
109 ---
110
111 This package including all files is subject to the LPPL 1.3c license.
112 Copyright 2024–2025 Jasper Habicht (mail(at)jasperhabicht.de).
113
114 Jason, the JSON parsing horse: Copyright 2024–2025 Hannah Klöber.