json ファイルの Grep 値

json ファイルの Grep 値

私のjsonファイルは次のようになります

[{"product":"Apple","id":"2134"},{"product":"Mango","id":"4567"}]

与えられた「製品」を見つけるためにgrep / sed / awk 'id'を使用したいと思います。

出力:

Enter product : Apple
Your product id is : 2134

ベストアンサー1

JSON認識ツールを使用してください。 PerlはJSON図書館:

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

use JSON;

my $json = '[{"product":"Apple","id":"2134"},{"product":"Mango","id":"4567"}]';

print 'Enter product: ';
chomp( my $product = <> );

print 'Your product id is: ', 
    (grep $_->{product} eq 'Apple', @{ from_json($json) })[0]{id}, "\n";

おすすめ記事