MC, 2025
Ilustracja do artykułu: Fortran JSON: How to Work with JSON in Fortran?

Fortran JSON: How to Work with JSON in Fortran?

Fortran, one of the oldest programming languages, continues to evolve. While it was primarily designed for numerical computing, modern software development often requires working with JSON. In this article, we'll explore how to handle JSON data in Fortran, providing practical examples and useful libraries to make your coding experience smoother.

Why Use JSON in Fortran?

JSON (JavaScript Object Notation) is a widely used data format, known for its simplicity and compatibility with web technologies. While Fortran is not traditionally associated with handling structured text-based data, many applications—such as scientific computing, data exchange, and configuration files—benefit from JSON.

Fortran JSON Libraries

Since Fortran does not have built-in support for JSON, several third-party libraries have been developed to bridge the gap. The most notable ones include:

  • json-fortran – A widely used Fortran library for reading and writing JSON files.
  • FSON – A lightweight, high-performance JSON parser for Fortran.
  • FLAP – Primarily a command-line parser, but it supports JSON-like data structures.

Installing json-fortran

The most popular Fortran JSON library is json-fortran. To install it, you can use a package manager like Spack:

spack install json-fortran

Alternatively, you can compile it from source:

git clone https://github.com/jacobwilliams/json-fortran.git
cd json-fortran
mkdir build
cd build
cmake ..
make
make install

Fortran JSON Examples

Now, let's look at some practical examples of working with JSON in Fortran.

Reading a JSON File

Assuming we have a JSON file called data.json:

{
  "name": "Fortran",
  "version": 90,
  "features": ["fast", "efficient", "scientific"]
}

We can read it using json-fortran as follows:

program read_json
  use json_module
  implicit none
  type(json_file) :: json
  type(json_value), pointer :: root
  character(len=:), allocatable :: name
  integer :: version

  call json%initialize()
  call json%load_file('data.json', root)

  call root%get('name', name)
  call root%get('version', version)

  print *, "Language: ", name
  print *, "Version: ", version

  call json%destroy()
end program read_json

Writing a JSON File

To create and save JSON data in Fortran:

program write_json
  use json_module
  implicit none
  type(json_file) :: json
  type(json_value), pointer :: root

  call json%initialize()
  call json%create_object(root)

  call root%add('name', 'Fortran')
  call root%add('version', 90)
  call root%add('features', ["fast", "efficient", "scientific"])

  call json%save_file('output.json', root)
  call json%destroy()
end program write_json

Parsing JSON Strings in Fortran

Sometimes, you may need to parse JSON data from a string instead of a file. Here's how:

program parse_json_string
  use json_module
  implicit none
  type(json_file) :: json
  type(json_value), pointer :: root
  character(len=:), allocatable :: json_string

  json_string = '{ "language": "Fortran", "year": 1957 }'

  call json%initialize()
  call json%parse(json_string, root)

  print *, "Language: ", root%get('language')
  print *, "Year: ", root%get('year')

  call json%destroy()
end program parse_json_string

Conclusion

Working with JSON in Fortran is easier than it may seem at first. With libraries like json-fortran, you can easily parse, read, and write JSON data in your Fortran applications. Whether you're dealing with scientific computing, web APIs, or configuration files, JSON support in Fortran makes your programs more flexible and interoperable.

So, next time you need structured data in your Fortran project, don't hesitate to use JSON!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

Imię:
Treść: