github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/types/json.md (about) 1 # `json` 2 3 > JavaScript Object Notation (JSON) 4 5 ## Description 6 7 JSON is a structured data-type within Murex. It is the standard format for all 8 structured data within Murex however other formats such as YAML, TOML and CSV 9 are equally first class citizens. 10 11 ## Examples 12 13 Example JSON document taken from [Wikipedia](https://en.wikipedia.org/wiki/JSON) 14 15 ``` 16 { 17 "firstName": "John", 18 "lastName": "Smith", 19 "isAlive": true, 20 "age": 27, 21 "address": { 22 "streetAddress": "21 2nd Street", 23 "city": "New York", 24 "state": "NY", 25 "postalCode": "10021-3100" 26 }, 27 "phoneNumbers": [ 28 { 29 "type": "home", 30 "number": "212 555-1234" 31 }, 32 { 33 "type": "office", 34 "number": "646 555-4567" 35 }, 36 { 37 "type": "mobile", 38 "number": "123 456-7890" 39 } 40 ], 41 "children": [], 42 "spouse": null 43 } 44 ``` 45 46 ## Detail 47 48 ### Tips when writing JSON inside for loops 49 50 One of the drawbacks (or maybe advantages, depending on your perspective) of 51 JSON is that parsers generally expect a complete file for processing in that 52 the JSON specification requires closing tags for every opening tag. This means 53 it's not always suitable for streaming. For example 54 55 ``` 56 » ja [1..3] -> foreach i { out ({ "$i": $i }) } 57 { "1": 1 } 58 { "2": 2 } 59 { "3": 3 } 60 ``` 61 62 **What does this even mean and how can you build a JSON file up sequentially?** 63 64 One answer if to write the output in a streaming file format and convert back 65 to JSON 66 67 ``` 68 » ja [1..3] -> foreach i { out (- "$i": $i) } 69 - "1": 1 70 - "2": 2 71 - "3": 3 72 73 » ja [1..3] -> foreach i { out (- "$i": $i) } -> cast yaml -> format json 74 [ 75 { 76 "1": 1 77 }, 78 { 79 "2": 2 80 }, 81 { 82 "3": 3 83 } 84 ] 85 ``` 86 87 **What if I'm returning an object rather than writing one?** 88 89 The problem with building JSON structures from existing structures is that you 90 can quickly end up with invalid JSON due to the specifications strict use of 91 commas. 92 93 For example in the code below, each item block is it's own object and there are 94 no `[ ... ]` encapsulating them to denote it is an array of objects, nor are 95 the objects terminated by a comma. 96 97 ``` 98 » config -> [ shell ] -> formap k v { $v -> alter /Foo Bar } 99 { 100 "Data-Type": "bool", 101 "Default": true, 102 "Description": "Display the interactive shell's hint text helper. Please note, even when this is disabled, it will still appear when used for regexp searches and other readline-specific functions", 103 "Dynamic": false, 104 "Foo": "Bar", 105 "Global": true, 106 "Value": true 107 } 108 { 109 "Data-Type": "block", 110 "Default": "{ progress $PID }", 111 "Description": "Murex function to execute when an `exec` process is stopped", 112 "Dynamic": false, 113 "Foo": "Bar", 114 "Global": true, 115 "Value": "{ progress $PID }" 116 } 117 { 118 "Data-Type": "bool", 119 "Default": true, 120 "Description": "ANSI escape sequences in Murex builtins to highlight syntax errors, history completions, {SGR} variables, etc", 121 "Dynamic": false, 122 "Foo": "Bar", 123 "Global": true, 124 "Value": true 125 } 126 ... 127 ``` 128 129 Luckily JSON also has it's own streaming format: JSON lines (`jsonl`). We can 130 `cast` this output as `jsonl` then `format` it back into valid JSON: 131 132 ``` 133 » config -> [ shell ] -> formap k v { $v -> alter /Foo Bar } -> cast jsonl -> format json 134 [ 135 { 136 "Data-Type": "bool", 137 "Default": true, 138 "Description": "Write shell history (interactive shell) to disk", 139 "Dynamic": false, 140 "Foo": "Bar", 141 "Global": true, 142 "Value": true 143 }, 144 { 145 "Data-Type": "int", 146 "Default": 4, 147 "Description": "Maximum number of lines with auto-completion suggestions to display", 148 "Dynamic": false, 149 "Foo": "Bar", 150 "Global": true, 151 "Value": "6" 152 }, 153 { 154 "Data-Type": "bool", 155 "Default": true, 156 "Description": "Display some status information about the stop process when ctrl+z is pressed (conceptually similar to ctrl+t / SIGINFO on some BSDs)", 157 "Dynamic": false, 158 "Foo": "Bar", 159 "Global": true, 160 "Value": true 161 }, 162 ... 163 ``` 164 165 #### `foreach` will automatically cast it's output as `jsonl` _if_ it's STDIN type is `json` 166 167 ``` 168 » ja [Tom,Dick,Sally] -> foreach name { out Hello $name } 169 Hello Tom 170 Hello Dick 171 Hello Sally 172 173 » ja [Tom,Dick,Sally] -> foreach name { out Hello $name } -> debug -> [[ /Data-Type/Murex ]] 174 jsonl 175 176 » ja [Tom,Dick,Sally] -> foreach name { out Hello $name } -> format json 177 [ 178 "Hello Tom", 179 "Hello Dick", 180 "Hello Sally" 181 ] 182 ``` 183 184 ## Default Associations 185 186 * **Extension**: `json` 187 * **MIME**: `application/json` 188 * **MIME**: `application/x-json` 189 * **MIME**: `text/json` 190 * **MIME**: `text/x-json` 191 192 193 ## Supported Hooks 194 195 * `Marshal()` 196 Writes minified JSON when no TTY detected and human readable JSON when stdout is a TTY 197 * `ReadArray()` 198 Works with JSON arrays. Maps are converted into arrays 199 * `ReadArrayWithType()` 200 Works with JSON arrays. Maps are converted into arrays. Elements data-type in Murex mirrors the JSON type of the element 201 * `ReadIndex()` 202 Works against all properties in JSON 203 * `ReadMap()` 204 Works with JSON maps 205 * `ReadNotIndex()` 206 Works against all properties in JSON 207 * `Unmarshal()` 208 Supported 209 * `WriteArray()` 210 Works with JSON arrays 211 212 ## See Also 213 214 * [`[[ Element ]]`](../parser/element.md): 215 Outputs an element from a nested structure 216 * [`cast`](../commands/cast.md): 217 Alters the data type of the previous function without altering it's output 218 * [`format`](../commands/format.md): 219 Reformat one data-type into another data-type 220 * [`hcl`](../types/hcl.md): 221 HashiCorp Configuration Language (HCL) 222 * [`jsonc`](../types/jsonc.md): 223 Concatenated JSON 224 * [`jsonl`](../types/jsonl.md): 225 JSON Lines 226 * [`open`](../commands/open.md): 227 Open a file with a preferred handler 228 * [`pretty`](../commands/pretty.md): 229 Prettifies JSON to make it human readable 230 * [`runtime`](../commands/runtime.md): 231 Returns runtime information on the internal state of Murex 232 * [`toml`](../types/toml.md): 233 Tom's Obvious, Minimal Language (TOML) 234 * [`yaml`](../types/yaml.md): 235 YAML Ain't Markup Language (YAML) 236 * [index](../parser/item-index.md): 237 Outputs an element from an array, map or table 238 * [mxjson](../types/mxjson.md): 239 Murex-flavoured JSON (deprecated) 240 241 ### Read more about type hooks 242 243 - [`ReadIndex()` (type)](../apis/ReadIndex.md): Data type handler for the index, `[`, builtin 244 - [`ReadNotIndex()` (type)](../apis/ReadNotIndex.md): Data type handler for the bang-prefixed index, `: Read from a data type one array element at a time 246 - [`WriteArray()` (type)](../apis/WriteArray.md): Write a data type, one array element at a time 247 - [`ReadMap()` (type)](../apis/ReadMap.md): Treat data type as a key/value structure and read its contents 248 - [`Marshal()` (type)](../apis/Marshal.md): Converts structured memory into a structured file format (eg for stdio) 249 - [`Unmarshal()` (type)](../apis/Unmarshal.md): Converts a structured file format into structured memory 250 251 <hr/> 252 253 This document was generated from [builtins/types/json/json_doc.yaml](https://github.com/lmorg/murex/blob/master/builtins/types/json/json_doc.yaml).