Although I have worked with Perl before I have not done any significant programming in it. I currently have a problem which seems like it should have a more elegant solution that I can come up with, but I am not familiar enough with parsing data to know.
I have a data file which I receive from a third party of the format:
[{“a”:”24”,”b”:”2014-01-15”,”c”:”English”,”d”:”3Y”}, {“a”:”21”,”b”:”2014-01-12”,”c”:”English”,”d”:”5Y”}, {“a”:”12”,”b”:”2013-12-15”,”c”:”French”,”d”:”7Y”}, {“a”:”18”,”b”:”2014-01-15”,”c”:”German”,”d”:”15Y”}]
Where the entire file is a single line and I have absolutely no control over this format.
I need to end up with a two dimensional Hash where the first dimension is keyed from the value in the ‘d’ field and the other fields are key/value pairs.
For example for the first record for this data:
my $one = 24
my $two = 2014-01-15
my $three = English
my $four = 3Y
The final hash would look like
my %Data
$Data{$four}{‘a’} = $one;
$Data{$four}{‘b’} = $two;
$Data{$four}{‘c’} = $three;
I know I can use “split (‘}’, $_);” to break the line into related sections. I could then further split this on “,” and “:” and take substrings to remove quotes and braces, and then fill everything in; but it seems like there should be an easier, more direct method, or maybe that is just wishful thinking on my part.
Can anyone suggest an easier method of reading a line of this format into a structure, which if not the final one that I am looking for, would at least allow me to drop some of the splits and/or substring commands it looks like I am going to need? Or just confirm that yes, I really do need to do this with numerous nested split commands.
Thanks in advance for any help.
I have a data file which I receive from a third party of the format:
[{“a”:”24”,”b”:”2014-01-15”,”c”:”English”,”d”:”3Y”}, {“a”:”21”,”b”:”2014-01-12”,”c”:”English”,”d”:”5Y”}, {“a”:”12”,”b”:”2013-12-15”,”c”:”French”,”d”:”7Y”}, {“a”:”18”,”b”:”2014-01-15”,”c”:”German”,”d”:”15Y”}]
Where the entire file is a single line and I have absolutely no control over this format.
I need to end up with a two dimensional Hash where the first dimension is keyed from the value in the ‘d’ field and the other fields are key/value pairs.
For example for the first record for this data:
my $one = 24
my $two = 2014-01-15
my $three = English
my $four = 3Y
The final hash would look like
my %Data
$Data{$four}{‘a’} = $one;
$Data{$four}{‘b’} = $two;
$Data{$four}{‘c’} = $three;
I know I can use “split (‘}’, $_);” to break the line into related sections. I could then further split this on “,” and “:” and take substrings to remove quotes and braces, and then fill everything in; but it seems like there should be an easier, more direct method, or maybe that is just wishful thinking on my part.
Can anyone suggest an easier method of reading a line of this format into a structure, which if not the final one that I am looking for, would at least allow me to drop some of the splits and/or substring commands it looks like I am going to need? Or just confirm that yes, I really do need to do this with numerous nested split commands.
Thanks in advance for any help.