File Exchange Pick of the Week

Our best user submissions

Parsing JSON files 6

Posted by Jiro Doke,

Jiro's pick this week is JSONlab by Qianqian Fang.

Recently, I've been working quite a bit with JSON files. I needed a way to read the files and parse out some content. With structured files like these, it's not terribly difficult to find tags and extract out information. But it does take some time. I started to write a program to do this.

What was I thinking?! Why re-invent the wheel when, most likely, someone has already done this before. It turns out that there are already a few File Exchange entries for dealing with JSON formats. Almost any of them would have helped me, but Qianqian's JSONlab caught my eyes. His entry directly handled JSON files, as opposed to JSON strings.

Let's see it in action. Suppose that there is a JSON file, "example.json", whose content is this.

     "firstName": "John",
     "lastName": "Smith",
     "age": 25,
         "streetAddress": "3 Apple Hill Dr",
         "city": "Natick",
         "state": "MA",
         "postalCode": "01760"
           "type": "home",
           "number": "123 456 7890"
           "type": "cell",
           "number": "098 765 4321"

You can extract the content like this.

data = loadjson('example.json');

Let's take a look.

      firstName: 'John'
       lastName: 'Smith'
            age: 25
        address: [1x1 struct]
    phoneNumber: {[1x1 struct]  [1x1 struct]}
    streetAddress: '3 Apple Hill Dr'
             city: 'Natick'
            state: 'MA'
       postalCode: '01760'
      type: 'home'
    number: '123 456 7890'
      type: 'cell'
    number: '098 765 4321'

In addition to decoding JSON files into MATLAB structures, the package lets you convert MATLAB structures into JSON format strings, as well as deal with binary JSON formats.

Thanks Qianqian! You saved me some time with my work!


Give this a try and let us know what you think here, or leave a comment for Qianqian.

Get the MATLAB code

Published with MATLAB® R2015a


Comments are closed.

6 CommentsOldest to Newest

Toshi Takeuchi replied on : 1 of 6

I am also a big fan of JSONlab, but lately I use webread() introduced in R2014b more and more if I am only interested in consuming JSON objects return from APIs, and webwrite() for posting MATLAB variables in JSON format to the web. One advantage for doing so is that it makes it easy to share code with other people.

Jiro Doke replied on : 2 of 6

Hi Toshi,

Thanks for mentioning webread and webwrite. For me, unfortunately, the JSON files were created locally and existed on my computer as files, so I couldn’t use these functions. You’re right, I have to make sure to include the extra functions when I share this with others. I’ll perhaps make it a Toolbox or an App.

Freeb replied on : 4 of 6

Hello. I suppose (B)JSON and YAML files are more and more popular in scientific community.
At some point, it will be great if Matlab have built-in support of these formats.

Muthu replied on : 5 of 6

Really this JSON loader is parsing JSON into live MATLAB objects, in a recursive descent fashion. So if you have a pretty deep JSON nested structure, this loader should giveup I assume due to stack overflow.

Maybe they will rewrite it in a iterative fashion. Maybe no.