3

I have a JSON file as below.

{
    "card":{
      "cardName":"10AN10G",
      "portSignalRates":[
         "10AN10G-1-OTU2",
         "10AN10G-1-OTU2E",
         "10AN10G-1-TENGIGE",
         "10AN10G-1-STM64"
      ],
      "listOfPort":{
         "10AN10G-1-OTU2":{
            "portAid":"10AN10G-1-OTU2",
            "signalType":"OTU2",
            "tabNames":[
               "PortDetails"
            ],
            "requestType":{
               "PortDetails":"PTP"
            },
            "paramDetailsMap":{
               "PortDetails":[
                  {
                     "type":"dijit.form.TextBox",
                     "name":"signalType",
                     "title":"Signal Rate",
                     "id":"",
                     "options":[

                     ],
                     "label":"",
                     "value":"OTU2",
                     "checked":"",
                     "enabled":"false",
                     "selected":""
                  },
                  {
                     "type":"dijit.form.TextBox",
                     "name":"userLabel",
                     "title":"Description",
                     "id":"",
                     "options":[

                     ],
                     "label":"",
                     "value":"",
                     "checked":"",
                     "enabled":"true",
                     "selected":""
                  },
                  {
                     "type":"dijit.form.Select",
                     "name":"Frequency",
                     "title":"Transmit Frequency"
                  }
               ]
            }
         }
      }
   }
}

I require the output to be:

signalType:"Signal Rate",
userLabel:"Description",
Frequency:"Transmit Frequency",.. ,.....

I tried with:

grep -oP '(?<=\"title\":\")[^"]*' file > outfile 

but this just splits the value of title and returns.

Can I use perl to access elements of the JSON data that I want?

4
  • Is JSON::Parse out of the question? Commented Jul 3, 2014 at 23:35
  • 1
    I don't think this is the problem but, just to let you know, your JSON you posted is invalid. Commented Jul 3, 2014 at 23:37
  • jq is a nifty command line filter for json of the sed/awk sort. stedolan.github.io/jq Commented Jul 4, 2014 at 1:33
  • I edited the post to make the JSON "valid". I used an online service and jq, json_verify to check the work. My thinking was a basic question like this might be useful to answer (even repeatedly), but that it needed proper data to be useful. I would not be surprised if it was a duplicate of another SO post (e.g. see this one on JSON and perl). But I think that is OK :-) Commented Jul 4, 2014 at 13:56

3 Answers 3

4

I suggest to use decode_json instead of a regex. First, install the JSON module from CPAN:

sudo perl -MCPAN -e 'install JSON'

Alternatively you can use apt-get on Ubuntu:

sudo apt-get install libjson-pp-perl

Once it is installed, you can use this code:

my $json = '{... your json string ...}';
my $decoded = decode_json($json);

$decoded->{'card'}{'listOfPort'}{'10AN10G-1-OTU2'}{'signalType'}

You can find more details in this article.

Sign up to request clarification or add additional context in comments.

Comments

2

Yes you can use the JSON perl module. Of course it meeds to be installed via cpan, cpanm or your system's packaging system. Parse the JSON into a hash and then use that in the normal way from Perl. Here's a quick example:

use JSON;
use IO::All;
use strict;
use warnings;

my $data < io '/tmp/data.json';
my $j = decode_json($data);

use DDP; # for quick debug printing

p $j->{card}{listOfPort}{"10AN10G-1-OTU2"}{paramDetailsMap}{PortDetails}[0]{title}
"Signal Rate"

You might want to use some deep diving techniques to get at the inner values more easily/programmatically (take a look at the Data::Diver module for that) - my example is meant to show only that it is possible and a bit of the mechanics of mapping JSON into a perl hash with the excellent JSON module. The documentation has lots of useful examples.

Comments

0

Perl on the command line:

perl -pe 's/"name":"([^"]+)","title":/"$1":/g' file > outfile

Output (pertinent part):

{"card": ... "signalType":"Signal Rate", ... "userLabel":"Description", ... "Frequency":"Transmit Frequency" ... }

1 Comment

Ah, okay. Updated response to execute with Perl on the command line.

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.