0

I would appreciate a sample of Java code that converts Perl hash into JSON.

Lots of Perl code can do this, but so far unable to find an equivalent Java code.

Sample hash:

  {
    'connection' => {
      'activate_on_startup' => 'y',
      'alert_snmp_trap' => 'N',
      'file' => 'H:\\HLC_170_Local_01\\logs\\win_os_profile.log',
      'data_source' => {
        'params' => {
          'name' => '
                     Processor\\% Processor Time\\_Total
                    ,System\\Context Switches/sec\\-1
                    ,Processor\\Interrupts/sec\\_Total
                    ,Processor\\% Privileged Time\\_Total
                    ,Processor\\% User Time\\_Total
                    ,Processor\\Interrupts/sec\\_Total
                    ,System\\Processor Queue Length\\-1 
                    ,System\\Context Switches/sec\\-1
                    ,Memory\\Page Faults/sec\\-1
                    ,Memory\\Pages/sec\\-1
                    ,Memory\\Transition Faults/sec\\-1
                    ,Memory\\Pages Input/sec\\-1
',
          'hostname' => ''
        },
        'name' => 'perfmon'
      },
      'charting_concentrator_index' => 1,
      'delta_flag' => '',
      'connection_type' => {
        'name' => 'capture'
      },
      'active_flag' => 'y',
      'cycle' => 5,
      'alert_capture_cycle' => 2
    },
    'name' => 'SYS_WIN_OS_PROFILE'
  },
Ron Warshawsky
  • 314
  • 2
  • 11
  • 2
    Can you show a sample of the configuration file? Why do you need to do it in Java? – Håkon Hægland Sep 06 '21 at 19:37
  • 2
    There's an old saying that says the only program capable of parsing Perl is perl. Good luck if you intend to venture down the path of properly parsing the syntax. – Silvio Mayolo Sep 06 '21 at 19:43
  • need to validate provided Perl configuration file in Java app – Ron Warshawsky Sep 06 '21 at 19:46
  • 2
    @RonWarshawsky "need to validate provided Perl configuration file in Java app"* yes but why not have the perl script read/write a json file instead. Then you can easily read that from Java also – Håkon Hægland Sep 06 '21 at 19:48
  • 3
    Are you sure that's a configuration file? It looks like a `Data::Dumper` output, which is intended for human consumption, not to be read by machines. Regardless, Perl is a programming language, not a configuration language, so I have several questions about your design process here. – Silvio Mayolo Sep 06 '21 at 19:50
  • 3
    Why can't you export the perl data structure (`$VAR1` in your example) to json and write it to a file and then read that file with java? – Ronaldo Ferreira de Lima Sep 06 '21 at 20:00
  • 2
    Are you saying that what you show sits in a file, exactly as shown? That's just a "dump" of a complex data structure; that's done for review, diagnostics, debugging and such, not to create a configuration file! That program can surely print a nice JSON instead. Can you clarify what this is that you show and whether you have access to that Perl program? – zdim Sep 06 '21 at 21:21
  • We do not have direct access to the Perl code, and the Java application is provided with the configuration file in Perl hash format. – Ron Warshawsky Sep 07 '21 at 23:57

1 Answers1

1

You don't have a Perl hash; You have Perl code. To parse it, you will need a Perl code parser. Writing would wold be a fantastic feat. Technically, being able to parse Perl code requires being able to run Perl code, so you would need to an entire Perl interpreter.

Now, this appears to be created using Data::Dumper, so you can rely on it being just a subset of Perl. Still, writing a parser for this is no small feat, and requesting one is way outside the scope of StackOverflow.

The simplest solution is to execute the following Perl one-liner to execute the code and serialize it using JSON:

perl -0777ne'
   use JSON qw( encode_json );
   eval($_);
   die $@ if $@;
   print(encode_json($VAR1));
'

It expects the code via STDIN, and outputs the JSON to its STDOUT. (The line breaks and spaces can be removed or left in.) I expect that this is easy to do in Java. But if you have problems with that, feel free to ask a question about that (after looking for existing questions on the subject).

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • It is indeed a configuration file provided in the format of the Perl hash dump. – Ron Warshawsky Sep 07 '21 at 23:59
  • No idea what that has to do with my answer – ikegami Sep 08 '21 at 00:01
  • Using Perl to do this is easy, but its integration with the Java core up will make the application much slower (calling Perl every time the new configuration file is shared or amended by 3rd party), plus the need to support Perl code in the production app. – Ron Warshawsky Sep 08 '21 at 00:05
  • 1
    Then use a sensible format. Data::Dumper is a debugging tool. – ikegami Sep 08 '21 at 00:09