Skip to content
Jacob Williams edited this page Oct 31, 2015 · 10 revisions

Example Usage

On this page, the basic functionality of the JSON-Fortran library is illustrated.

Reading JSON from a file

Reading a JSON file and getting data from it is fairly straightforward using the json_file class. Here is an example. See unit tests 1 and 3-6 for more examples. The source files may be found in src/tests/.

    program example1

        use json_module

        type(json_file) :: json
        logical :: found
        integer :: i,j,k

        ! initialize the module
        call json_initialize()

        ! read the file
        call json%load_file(filename = '../files/inputs/test1.json')

        ! print the file to the console
        call json%print_file()

        ! extract data from the file
        ! [found can be used to check if the data was really there]
        call json%get('version.major', i, found)
        if ( .not. found ) stop 1
        call json%get('version.minor', j, found)
        if ( .not. found ) stop 1
        call json%get('data(1).number', k, found)
        if ( .not. found ) stop 1

        ! clean up
        call json%destroy()
        if (json_failed()) stop 1

    end program example1

Reading JSON from a string

JSON can also be read directly from a character string like so:

    call json%load_from_string('{"name": "Leonidas"}')

Modifying variables in a JSON file

After reading a JSON file, if you want to change the values of some of the variables, you can use the update method. For the example above:

    ! [found can be used to check if the data was really there]
    call json%update('version.major',9,found)  !change major version to 9
    call json%update('version.minor',0,found)  !change minor version to 0
    call json%update('version.patch',0,found)  !change patch to 0

Writing a JSON file

To print the JSON file (either to a file or the console), the print_file method can be used. For the above example:

    call json%print_file()         !prints to the console
    call json%print_file(iunit)    !prints to the file connected to iunit

Building a JSON file from scratch

Constructing a JSON file element by element is slightly more complicated and involves the use of json_value pointers. For more examples see unit tests 2, 4 and 7 in src/tests/.

    program example2

        use,intrinsic :: iso_fortran_env, only: wp => real64

        use json_module

        type(json_value),pointer :: p, inp

        ! initialize the module
        call json_initialize()

        ! initialize the structure:
        call json_create_object(p,'')

        ! add an "inputs" object to the structure:
        call json_create_object(inp,'inputs')
        call json_add(p, inp) !add it to the root

        ! add some data to inputs:
        call json_add(inp, 't0', 0.1_wp)
        call json_add(inp, 'tf', 1.1_wp)
        call json_add(inp, 'x0', 9999.0000d0)
        call json_add(inp, 'integer_scalar', 787)
        call json_add(inp, 'integer_array', [2,4,99])
        call json_add(inp, 'names', ['aaa','bbb','ccc'])
        call json_add(inp, 'logical_scalar', .true.)
        call json_add(inp, 'logical_vector', [.true., .false., .true.])
        nullify(inp)  !don't need this anymore

        ! write the file:
        call json_print(p,'../files/example2.json')

        !cleanup:
        call json_destroy(p)
        if (json_failed()) stop 1

    end program example2

The code above produces the file:

{
  "inputs": {
    "t0": 0.1E+0,
    "tf": 0.11E+1,
    "x0": 0.9999E+4,
    "integer_scalar": 787,
    "integer_array": [
      2,
      4,
      99
    ],
    "names": [
      "aaa",
      "bbb",
      "ccc"
    ],
    "logical_scalar": true,
    "logical_vector": [
      true,
      false,
      true
    ]
  }
}