-3

I am Beginner to perl programming and I want know solution for this problem. I have this below information in the text file called token.txt. I want to extract only dynamically generated access_token value and store that value to mysql database.As mentioned access_token will be auto generated everytime so how I need to store this access_token value everytime. Anyone help me with the perl code. Thanks in advance

{
  "access_token" : "JgV8Ln1lRGE8JTz4olEQW0rJJHUYsq2LO8Ny9o6m",
  "token_type" : "abcdef",
  "expires_in" : 123456
}
PrakashG
  • 1,642
  • 5
  • 20
  • 30

2 Answers2

2

This is JSON formatted text, so I would suggest reading the file into a string and decoding it, e.g.:

parse.pl

use File::Slurp;
use v5.10;
use JSON;

$token = decode_json ( read_file('token.txt') );

say $token->{'access_token'};

Test it like this:

perl parse.pl

Output:

JgV8Ln1lRGE8JTz4olEQW0rJJHUYsq2LO8Ny9o6m
Thor
  • 45,082
  • 11
  • 119
  • 130
0

token will be in $tok,

perl -ne 's/"access_token"\s:\s"([^"]+)"/$tok=$1;print $1/e' token.txt