1

I have an issue when I try to execute a loop to handle a json file, I am trying to replace the Zero in "correct":0 to the right answer number tagged by {C} for example for Question 1 I need to put "correct":1 and for Question 2 I need to put "correct":3 and so on ...

this is my perl file :

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

binmode STDOUT, ":utf8";
use utf8;

use JSON;

my $json;
{
  local $/; #Enable 'slurp' mode
  open my $fh, "<", "test.json";
  $json = <$fh>;
  close $fh;
}
my $data = decode_json($json);
# Output to screen one of the values read
my $var = $data->{'question'}->{'answers'};
my ($correctans) = grep {s/.*(\d).*/$1/ if m/C/} (my @answers = qw($var));
# Modify the value, and write the output file as json
$data->{'question'}->{'correct'}->[0] = $correctans;
open my $fh, ">", "test.json";
print $fh encode_json($data);
close $fh;

my json file :

{
    "introduction":"This quiz tests you about foo and goo", 
    "questions":[
        {"question":"Why is the sky blue?", 
         "answers":["Unicorns{C}","Fairies","Boring Science","Kittens"],
         "correct":0},
        {"question":"Why are kittens so cute?", 
         "answers":["Magic","Fur","Meow{C}","More Kittens!"],
         "correct":0}
    ]
}

Any help ?

1 Answer 1

3
use JSON;

use strict;
use warnings;

my $json = do {local $/; <DATA>};

my $data = decode_json($json);

for my $questions (@{$data->{questions}}) {
    my $answers = $questions->{answers};
    my ($answer) = grep {$answers->[$_] =~ /\{C\}/} (0..$#$answers);
    $questions->{correct} = 1 + $answer;
}

use Data::Dump;
dd $data;

__DATA__
{
    "introduction":"This quiz tests you about foo and goo", 
    "questions":[
        {"question":"Why is the sky blue?", 
         "answers":["Unicorns{C}","Fairies","Boring Science","Kittens"],
         "correct":0},
        {"question":"Why are kittens so cute?", 
         "answers":["Magic","Fur","Meow{C}","More Kittens!"],
         "correct":0}
    ]
}

Outputs

{
  introduction => "This quiz tests you about foo and goo",
  questions    => [
                    {
                      answers  => ["Unicorns{C}", "Fairies", "Boring Science", "Kittens"],
                      correct  => 1,
                      question => "Why is the sky blue?",
                    },
                    {
                      answers  => ["Magic", "Fur", "Meow{C}", "More Kittens!"],
                      correct  => 3,
                      question => "Why are kittens so cute?",
                    },
                  ],
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @Miler , but how can use DATA from a file.json and save OUTPUT to output.json ?
Your code already demonstrates how to load data from a file and send it back when done processing. That part looked fine. You just needed help with how to process the parsed data structure, which this demonstrates.
@Miller++ cool cross platform object/ref accessing! @saad+- (grep my answer...) your question made far more sense when you posted examples. However your attempt to paste code relating to another given example to solve your un-shown problem did make me chuckle.

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.