0

I've two string like follows in PERL script.

$labels = "firstname|lastname|email";

$values = 'krishna|mohan|[email protected]';

Now I would like to construct a JSON fromat from those two srings. I need to split (explode) both the strings base on | (pipe) symbol and construct a JSON format like follows

{"firstname":"krishna","lastname":"mohan","email":"[email protected]"}

How can I achieve this? Any Ideas would be much appreciated.

1
  • 2
    @j{ split /\|/, $labels } = split /\|/, $values; Commented Nov 27, 2014 at 13:41

1 Answer 1

3

Use split to "explode" the strings, build a hash from the results. Then, use JSON to translate it to JSON:

#!/usr/bin/perl
use warnings;
use strict;

use JSON;

my $labels = 'firstname|lastname|email';
my $values = 'krishna|mohan|[email protected]'; # Doesn't work with double quotes!

my %hash;
@hash{ split /\|/, $labels } = split /\|/, $values;

print to_json(\%hash);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.