Tag Info

Hot answers tagged

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 ...


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 ...


12

The bug is in the System`Convert`JSONDump`toString[] 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 ...


12

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 ...


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", ...


8

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", \ ...


8

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: System`Convert`JSONDump`toString[num_?NumberQ, t_Integer] := StringReplace[ToString[CForm[N[num]]], RegularExpression["\\.(($)|(e))"] -> ".0$1"]; For the following input: ...


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


7

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] System`Convert`JSONDump`$JSONObjectHead = JSONObject; Now you can use ...


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 ...


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

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 ...


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

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" -> ...


5

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, ...


3

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[#, _, ...


3

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"}, ...


3

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 ...


2

Perhaps this might work to add the zeros after the decimal point: System`Convert`JSONDump`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

I like to use Cases with HoldPattern for these types of structures: Cases[out, HoldPattern["location" -> latlon_] :> {latlon[[1, 2]], latlon[[2, 2]]}, Infinity] {{48.3559, 10.8946}, {48.3747, 10.8957}}


1

Here's a workaround until this gets fixed. WARNING: Modifying installation files can break things. Do this at your own risk, and keep the modification in mind, in case you experience problems with JSON export later. Do this only if you are affected by the problem. Open the file SystemFiles`Links`JSONTools`JSONTools.m Skip to line 233, where the number ...


1

Because I actually had this problem but eventually solved it I thought I'd question and answer it myself. Feel free to submit a better solution! First, import the data using: json = Import["example.json","JSON"] The function that does all the magic is: JSONToAssociation[list_] := Association[ Table[ If[ ListQ[i[[2]]], i[[1]] -> ...


1

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", ...


1

That's a funny bug that I would report to Wolfram. I managed to similarly break my environment by trying to answer this :-p. However, functionality is restored for the current session if you run Get[FindFile["JSONTools`"]] Unfortunately I seem to need to run this whenever I restart the kernel.


1

Longtime user, first time responder. I am going to assume that by parsing you mean extracting values from JSON data and not transforming data into JSON. There are a couple ways to parse JSON in Mathematica. Assuming the data is already a set of rules in Mathematica using Import[data, "JSON"] or something like that. data = {blah -> blah, field1 ...



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