# Efficient JSON parser for Mathematica

Im looking for a efficient way of parsing JSON data in Mathematica. I am having a lot of problems with ImportString because it uses Java, is too slow, and uses too much memory (and, also, sometimes it just returns Null).

Do you know how to write a function for JSON parsing in Mathematica, or how to use any C/C++ (or other efficient) library from Mathematica?

Recent versions of Mathematica no longer use Java for parsing JSON, and work very fast.

There are at least three ways to read JSON.

• Import[..., "JSON"] is fast an reliable. It returns nested Lists and Rules.

• Import[..., "RawJSON"] is faster than "JSON" and returns nested Lists and Associations. These are much easier to work with than Rules.

• The Developer context has JSON reading and writing functions. There are functions for reading from files, streams or strings. There are functions for encoding/decoding Mathematica expressions (labelled Expression) and functions for reading/writing arbitrary JSON (labelled as Raw). These latter ones are essentially equivalent to Import[..., "RawJSON"].

Example: DeveloperReadRawJSONString[str]

These functions have much lower overhead than Import/Export because they do not have to invoke the entire import framework. In fact they are so fast that in some cases using them for data transfer with LibraryLink can be faster than using MathLink.

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 -> var, field2 -> var2, blah-> blah, ...}


The first is Cases

Cases[data,{____ ,"field1"->var_, "field2"->var2_, ____}-> {var,var2}, Infinity]


The second is replace.

dumvar = {"field1", "field2", ..} /. data

• By parsing, I mean converting a String (which is a JSON representation of some data) into a list that contains the data itself. – José D. Apr 21 '14 at 20:18