parse_json_string_array Subroutine

subroutine parse_json_string_array(line, key, values, n)

Parse a JSON string array from one line.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: line

Source line and array key token

character(len=*), intent(in) :: key

Source line and array key token

character(len=128), intent(inout) :: values(:)

Destination array for parsed values

integer, intent(inout) :: n

Number of valid parsed values


Source Code

  subroutine parse_json_string_array(line, key, values, n)
    !! Parse a JSON string array from one line.
    implicit none

    ! Arguments
    character(*), intent(in) :: line, key
    !! Source line and array key token
    character(128), intent(inout) :: values(:)
    !! Destination array for parsed values
    integer, intent(inout) :: n
    !! Number of valid parsed values

    ! Local variables
    integer :: pos, pos2, pos3
    !! Temporary indices while scanning quoted values

    pos = index(line, trim(key))
    if(pos .eq. 0) return

    pos = pos + len_trim(key)
    pos2 = index(line(pos:), '[')
    if(pos2 .eq. 0) return
    pos = pos + pos2

    do
       pos2 = index(line(pos:), '"')
       if(pos2 .eq. 0) exit
       pos = pos + pos2
       pos3 = index(line(pos:), '"')
       if(pos3 .eq. 0) exit
       n = n + 1
       values(n) = line(pos:pos+pos3-2)
       pos = pos + pos3
       if(index(line(pos:), ']') .gt. 0 .and. &
            index(line(pos:), '"') .eq. 0) exit
       if(index(line(pos:), ']') .gt. 0 .and. &
            index(line(pos:), ']') .lt. index(line(pos:), '"')) exit
    end do

  end subroutine parse_json_string_array