# Tag Info

21

Here's a set of functions that allows to do this. The code uses many ideas found on this site and on other places on the web. It is a bit factorized already so it should be easily reusable. More on YQL and available tables here: https://developer.yahoo.com/yql https://github.com/yql Query test Edit: this API is great also and simple http://www.quandl.com/...

18

Update: Athanassios correctly pointed out that my attempt to set defaults doesn't work. The problem appears to be that the absence of the element from the first list (ad) causes the defaults list to never be checked. A condensed exhibit: ad = {"accept_rate" -> 80, "badge_counts" -> {"bronze" -> 39, "silver" -> 9}}; defaults = {"age" -> ...

17

I separated this project into two parts. The first is to compute the coordinates of the Geohash location. (*Grab the user's geographical location. The location is based on IP address, so it may not be completely accurate. It's usually good enough to get your graticute. You can replace home with with known coordinates in the form {hx, hy} if you like.*) home ...

13

The bug is in the SystemConvertJSONDumptoString[] function which is defined like this: toString[num_?NumberQ, t_Integer] := If[Head[num] === Real && IntegerPart[num] == num, ToString[CForm[N[num]]] ~~ "0", ToString[CForm[N[num]]]] If my guess is correct, the purpose of the ~~ "0" part is to change 1. to 1.0. I am not very familiar with ...

13

I ran into the same issue. Dataset and nested Associations have a structure ideally suited for JSON export, but the JSON exporter only supports lists of rules. Any other type of expression triggers the error you see. Export The workaround is to recursively convert all nested associations to lists of rules. normalAsc[expr_] := expr //. a_Association :>...

10

See Szabolcs answer for the explanation. The number form there needs to be corrected because JSON needs a digit between . and e: Therefore, here the solution: SystemConvertJSONDumptoString[num_?NumberQ, t_Integer] := StringReplace[ToString[CForm[N[num]]], RegularExpression["\\.(($)|(e))"] -> ".0$1"]; For the following input: ExportString[{"a"...

10

You can define your own object and tell Mathematica to interpret it as JSON object. Dummy export to load relevant contexts: In[1]:= ExportString["", "JSON"]; Tell Mathematica to interpret JSONObject symbol as possible head of JSON objects: In[2]:= ClearAll[JSONObject] SystemConvertJSONDump\$JSONObjectHead = JSONObject; Now you can use ...

9

Element as referred to in the documentation are not a JSON elements, but the kind of information you can get from the specified file format. For JSON the elements available is just Data as given by Import["out.json", "Elements"]. For a typical JPEG photo the available elements are: {"Aperture", "BitDepth", "CameraTopOrientation", "ColorMap", \ "...

9

It is possible to get rid of all but one ReplaceAll. First, you need to remember that ReplaceAll tests each Rule in order, so to deal with the missing "badge_counts" condition, you can simply add that to the list, as follows {"display_name", "creation_date", "reputation", "reputation_change_week", "is_employee", "last_access_date", "user_type", "...

9

The result of importing a file in JSON format is a list of rules. For information about ways to use rules, see the documentation for Applying Transformation Rules. Here is an example: data = ImportString["{\"x\":1, \"y\":{\"a\":2, \"b\":[3, 4]}}", "JSON"] (* {"x" -> 1, "y" -> {"a" -> 2, "b" -> {3, 4}}} *) Retrieving a top-level property is ...

8

I don't believe that this is a bug. Here's why: What exactly happens? Export and ExportString will always encode "JSON" as UTF-8. This may be intentional, as UTF-8 is the default encoding for JSON files. From the documentation: Strings in the Wolfram Language are represented in JSON as UTF-8 strings, escaped as required by the JSON standard. ...

7

The reason is that the JSON format is quite limited. It doesn't support arbitrary expressions. You can e.g. see that by trying the folllowing: Export["test.json",someSymbol,"JSON"] You'll get the same error. If your goal is just to pass the expressions around (i.e. on the other side is another Mathematica instance to interpret them), the simplest ...

7

So if you have: file = Import[path] then after the file is not needed: Clear[file] e.g.: In[77]:= MemoryInUse[] file = Import[StringJoin[NotebookDirectory[], "IMG_3025.jpg"]]; MemoryInUse[] Clear[file]; MemoryInUse[] Out[77]= 79593488 Out[79]= 115619864 Out[81]= 79591456

6

The introduction of Assocation brings a powerful new way to handle this problem. Version 10.2 provides for import of JSON as nested associations without use of ToAssociations by using the format "RawJSON".   (* archive data *) ad = {"accept_rate" -> 75, "account_id" -> 395497, "age" -> 41, "badge_counts" -> {"bronze" -> 35, "...

6

The short answer is that, as @FJRA noted in the comment, only certain types are supported. Which types? Enter the long answer. Why the converter behaves as it does Long answer: JSON supports only certain types, and their nested combinations, as defined e.g. here. Mathematica converter maps JSON objects to lists of rules, arrays to lists, strings to strings,...

6

JSON stands for JavaScript Object Notation. In JavaScript, and hence in JSON, the ordering of dictionary keys is not determined, and not meaningful. Thus Mathematica's behaviour is correct. From the JavaScript language specification, An Object is an unordered collection of properties. Each property consists of a name, a value and a set of attributes. ...

6

tl;dr I think it's a memory leak (bug) and you should report it to Wolfram Support (please do!) According to my reading, you were saying that after importing JSON files many times, the kernel memory usage reported by the operating system (or some task manager program) was growing to unreasonable levels. However, the memory usage reported by the kernel ...

5

With Mathematica 10 or later, I recommend converting this data structure to either an Association or a Dataset: asc = Replace[out, r : {__Rule} :> Association[r], {0, Infinity}]; ds = Dataset[asc] Then you can index it with the keys, e.g. asc[["results", 1, "geometry"]] (* <|"location" -> <|"lat" -> 48.35593, "lng" -> 10.89459|>|>...

5

This seems to be a Java heap space error. I encounter this quite often when importing large Excel sheets, though I get a different error message: Import::nojmem. The help page for this error contains instructions that may be useful for you as well. Basically, you increase heap space using the following commands: Needs["JLink"] ReinstallJava[JVMArguments -&...

5

Here's a solution using your example file. Import as RawJSON because this gives us much easier to manipulate associations. json = Import[ "https://raw.githubusercontent.com/d3/d3-plugins/master/graph/data/miserables.json", "RawJSON"] After inspecting the format manually, it's easy to extract vertices: vertices = json[["nodes", All, "name"]] Links ...

4

For newer versions exporting an empty Association does what you want: ExportString[<||>, "JSON"] Newer Versions means >= 10.1, 10.0.x versions did export empty Associations to empty lists, of course the new behavior is a much better match for the distinction of JavaScript empty lists and objects in Mathematica...

4

refering to my answer your other question here is how you can get a table of e.g. coordinates and temperature from your example data (adopt to your needs): url = "http://api.openweathermap.org/data/2.5/find?lat=55.5&lon=37.5&cnt=10"; data = Import[url,"JSON"] //. x : {__Rule} :> Association[x]; Map[ (# @@@ {{"coord","lat"}, {"coord","lon"}, {"...

4

If you know what the JSON you receive looks like your own approach might work well enough. On the other hand JSON might not necessarily contain just nested "Objects" but also arrays, in which case it will cause errors or invalid unevaluated Assocation-expressions if one assumes that every list resulting from the imported JSON is a list of rules (or even has ...

4

Something like this is a good start. I tried to write it in a way to make it easy to expand. ruleListQ[{r__Rule}] = True; (* JSON won't have RuleDelayed *) ruleListQ[_] = False; formatJSON[json_] := Switch[json, _?ruleListQ, (* dictionary *) Column@Replace[json, HoldPattern[a_ -> b_] :> OpenerView[{a, formatJSON[b]}], {1}], _?(ArrayQ[#, _, ...

4

4

If we import the data as text, we can see that it is not valid JSON: json = Import["http://www.google.com/finance/option_chain?q=AAPL&output=json", "Text"]; json // Short (* {expiry:{y:2016,m:3,d:11},expirations... g_id:"22144",underlying_price:102.26} *) The keys are not quoted despite the requirement of the JSON standard. This is a well-known ...

3

Actually this problem is still in Mma 10.2 here is a solution ExportString["Conexión", "JSON"] (* "ConexiÃ\.b3n" *) ExportString["Conexión", "JSON", CharacterEncoding -> "UTF8"] (* "ConexiÃ\.b3n" *) FromCharacterCode[ ToCharacterCode[ExportString["Conexión", "JSON"]], "UTF8"] (* "Conexión" *) ExportString["Conexión", "RawJSON"] (* ...

2

Perhaps this might work to add the zeros after the decimal point: SystemConvertJSONDump`toString[num_?NumberQ,t_Integer]:= StringReplace[ StringReplace[ ToString[CForm[N[num]]], RegularExpression["\\.&"]->".0" ], ".e"->".0e" ] I cannot check, but this seems to comply with the format descriptions provided by hwlau.

2

This is unfortunately not an answer, but an extended comment that outgrew the comment form. In short, this might be a bug in ExportString. I am using Mathematica v. 10.1.0 on Windows 7 - 64 bit. Here's what I tried so far. I tried to export your JSON content to a file, using the same format you proposed. This works just fine. Export["fromExport.json", {...

2

This can be done with Clock. The following will import foo.json every 5 seconds. With[{update = 5}, Dynamic[{foo = Import["~/Desktop/foo.json"], Clock[{1, update, update}, update]}]]

Only top voted, non community-wiki answers of a minimum length are eligible