1json(n) JSON json(n)
2
3
4
5______________________________________________________________________________
6
8 json - JSON parser
9
11 package require Tcl 8.4
12
13 package require json ?1.3.4?
14
15 ::json::json2dict txt
16
17 ::json::many-json2dict txt ?max?
18
19______________________________________________________________________________
20
22 The json package provides a simple Tcl-only library for parsing the
23 JSON http://www.json.org/ data exchange format as specified in RFC 4627
24 http://www.ietf.org/rfc/rfc4627.txt. There is some ambiguity in pars‐
25 ing JSON because JSON has type information that is not maintained by
26 the Tcl conversion. The json package returns data as a Tcl dict. Ei‐
27 ther the dict package or Tcl 8.5 is required for use.
28
30 ::json::json2dict txt
31 Parse JSON formatted text txt into a Tcl dict and return the
32 value.
33
34 If txt contains more than one JSON entity only the first one is
35 returned.
36
37 ::json::many-json2dict txt ?max?
38 Parse JSON formatted text txt containing multiple JSON entities
39 into a list of dictionaries and return that list.
40
41 If max is specified exactly that many entities are extracted
42 from txt. By default the command will attempt to extract all,
43 without limits. A value of "max == 0" does not make sense and
44 will cause the command to throw an error.
45
47 An example of a JSON array converted to Tcl. A JSON array is returned
48 as a single item with multiple elements.
49
50 [
51 {
52 "precision": "zip",
53 "Latitude": 37.7668,
54 "Longitude": -122.3959,
55 "Address": "",
56 "City": "SAN FRANCISCO",
57 "State": "CA",
58 "Zip": "94107",
59 "Country": "US"
60 },
61 {
62 "precision": "zip",
63 "Latitude": 37.371991,
64 "Longitude": -122.026020,
65 "Address": "",
66 "City": "SUNNYVALE",
67 "State": "CA",
68 "Zip": "94085",
69 "Country": "US"
70 }
71 ]
72 =>
73 {Country US Latitude 37.7668 precision zip State CA City {SAN FRANCISCO} Address {} Zip 94107 Longitude -122.3959} {Country US Latitude 37.371991 precision zip State CA City SUNNYVALE Address {} Zip 94085 Longitude -122.026020}
74
75
76 An example of a JSON object converted to Tcl. A JSON object is re‐
77 turned as a multi-element list (a dict).
78
79 {
80 "Image": {
81 "Width": 800,
82 "Height": 600,
83 "Title": "View from 15th Floor",
84 "Thumbnail": {
85 "Url": "http://www.example.com/image/481989943",
86 "Height": 125,
87 "Width": "100"
88 },
89 "IDs": [116, 943, 234, 38793]
90 }
91 }
92 =>
93 Image {IDs {116 943 234 38793} Thumbnail {Width 100 Height 125 Url http://www.example.com/image/481989943} Width 800 Height 600 Title {View from 15th Floor}}
94
95
97 To write json, instead of parsing it, see package json::write.
98
100 This document, and the package it describes, will undoubtedly contain
101 bugs and other problems. Please report such in the category json of
102 the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please
103 also report any ideas for enhancements you may have for either package
104 and/or documentation.
105
106 When proposing code changes, please provide unified diffs, i.e the out‐
107 put of diff -u.
108
109 Note further that attachments are strongly preferred over inlined
110 patches. Attachments can be made by going to the Edit form of the
111 ticket immediately after its creation, and then using the left-most
112 button in the secondary navigation bar.
113
115 data exchange, exchange format, javascript, json
116
118 CGI programming
119
121 Copyright (c) 2006 ActiveState Software Inc.
122 Copyright (c) 2009 Thomas Maeder, Glue Software Engineering AG
123
124
125
126
127tcllib 1.3.4 json(n)