Taking data from one Json and saving to another [Perl]

Good night, I would like to know if there is a way to take data from one json and save in another in an automatic way without having to treat the json as "text" (regex) in case, I have already used the Perl JSON module (use JSON; ) but it was for a simpler utility, I have done some searches and

The json I'm going to "take" the information from is like this:

[{"name":"AK-47 | Aquamarine Revenge (Battle-Scarred)","price":1292,"have":10,"max":28},{"name":"AK-47 | Aquamarine Revenge (Factory New)","price":3769,"have":4,"max":13}]

I would like to pass only the entries that are inside "name" and "price", getting more or less like this:

{"AK-47 | Aquamarine Revenge (Battle-Scarred)":"13.16",
"AK-47 | Aquamarine Revenge (Factory New)":"37.64"}]

I have already managed something by treating json as text, but it is not getting "good" as it is dealing with the file. Any example or idea is already welcome for me to start looking for how to solve.

I thank you for your help.

 1
Author: edufgimenez, 2017-09-19

1 answers

With Perl + JSON module:

#!/usr/bin/perl 
use JSON;
my $j = '[
  {"name":"AK-47 | Aquamarine Revenge (Battle-Scarred)",
   "price":1292,
   "have":10,
   "max":28},
  {"name":"AK-47 | Aquamarine Revenge (Factory New)",
   "price":3769,
   "have":4,
   "max":13}]';

my  $v=from_json($j);
print to_json( {map {($_->{name},$_->{price})}  @$v });

By the way another alternative command jq

$ cat x.json | jq 'map([ .name , .price])'
[
  [
    "AK-47 | Aquamarine Revenge (Battle-Scarred)",
    1292
  ],
  [
    "AK-47 | Aquamarine Revenge (Factory New)",
    3769
  ]
]
 2
Author: JJoao, 2017-09-27 12:14:47