extract_json_string Subroutine

subroutine extract_json_string(line, key, value)

Extract a string value from a JSON key-value pair.

Arguments

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

Source line and key token to find

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

Source line and key token to find

character(len=*), intent(out) :: value

Extracted string value


Source Code

  subroutine extract_json_string(line, key, value)
    !! Extract a string value from a JSON key-value pair.
    implicit none

    ! Arguments
    character(*), intent(in) :: line, key
    !! Source line and key token to find
    character(*), intent(out) :: value
    !! Extracted string value

    ! Local variables
    integer :: pos, pos2, pos3
    !! Temporary indices used while slicing quoted text

    value = ''
    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
    pos3 = index(line(pos:), '"')
    if(pos3 .eq. 0) return
    value = line(pos:pos+pos3-2)

  end subroutine extract_json_string