gurpreetsingh13 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks,
I want to convert a perl data structure into JSON.
For that, I need to convert an epoch datetime of milliseconds to JSON type. Following is the initial code.
Problem is that I want a single backslash with the boundaries of word "Date", but each time it gets converted to 2 backslashes.
Couldn't find any direct way to convert a date into JSON as we do with JSON::PP::true or other things. Quite confused with this.
my $roundParams = { 'AllowParticipantsToSeeEachOther' => JSON::PP:: +true, 'DurationInMinutes' => 10, 'MaxGamePegValue' => 5, 'MaxNumberOfCrackAttemptsPerCode' => 18, 'MaxNumberOfCrackAttemptsPerTeam' => 0, 'NumberOfDefaultHostParticipants' => 1, 'NumberOfPegsInRound' => 4, 'RoundStartTime' => "\\/Date(" + . $milliSeconds . ")\\/", 'TimeBetweenRoundATimeBetweenRoundAttemptsInSeconds' => 10 }; say Dumper $roundParams; say $roundParams->{'RoundStartTime'}; say to_json $roundParams;

Output:

{"AllowParticipantsToSeeEachOther":true,"TimeBetweenRoundATimeBetweenR +oundAttemptsInSeconds":10,"NumberOfDefaultHostParticipants":1,"MaxNum +berOfCrackAttemptsPerTeam":0,"RoundStartTime":"\\/Date(1401265928689) +\\/","NumberOfPegsInRound":4,"DurationInMinutes":10,"MaxNumberOfCrack +AttemptsPerCode":18,"MaxGamePegValue":5}
Tried various things like eliminating one backslash for escape character, adding \e, using qq etc., but nothing worked out. Would be thankful if you could guide in that.

Replies are listed 'Best First'.
Re: Json epoch datetime with perl
by hdb (Monsignor) on May 28, 2014 at 09:26 UTC

    The immediate question I have is what you would need the backslash for. It sounds a bit like an XY Problem to me. You said you really wanted a date field but that still does not tell me what the backslash is good for.

      I want to pass this JSON object to a Web Service and the example structure given for the object is showing the similar kind of backslashes. Here is the exact copy paste of that field. Date is in epoch milliseconds.
      "RoundStartTime":"\/Date(1400569278222)\/",
        Try
        #!perl; use strict; use JSON; my $json = new JSON->escape_slash(); my $roundParams = { 'AllowParticipantsToSeeEachOther' => JSON::PP::true, 'DurationInMinutes' => 10, 'MaxGamePegValue' => 5, 'MaxNumberOfCrackAttemptsPerCode' => 18, 'MaxNumberOfCrackAttemptsPerTeam' => 0, 'NumberOfDefaultHostParticipants' => 1, 'NumberOfPegsInRound' => 4, 'RoundStartTime' => "/Date(".time.")/", 'TimeBetweenRoundATimeBetweenRoundAttemptsInSeconds' => 10 }; print $json->encode($roundParams);
        poj
Re: Json epoch datetime with perl
by Anonymous Monk on May 28, 2014 at 08:54 UTC
    JSON doesn't have a Date type, so you're not dealing with JSON format, so you don't want JSON module, you want something else, :)
      Agreed. It is just that I am treating that "\/Date(<epoch>)\/" as a basic string enclosed in double quotes. My problem is with those single backslashes getting replaces by double backslashes.

        Agreed. It is just that I am treating that "\/Date(<epoch>)\/" as a basic string enclosed in double quotes. My problem is with those single backslashes getting replaces by double backslashes.

        Um no. In the OP you have double backslashes; you start with double backslashes;

        use JSON qw/ to_json /; my @fudge = "\/Date(<epoch>)\/"; print to_json( \@fudge ); __END__ ["/Date(<epoch>)/"]