2

I'm on PHP and I need to edit a JSON output to return only objects >=0 and divided by one hundred

Eg.

$json = {"data":[0,55,78,-32,-46,37]}

Needed

$json = {"data":[0,0.55,0.78,0.37]}

How this can be done?

  • 1
    http://php.net/manual/en/function.json-decode.php – Sammitch Aug 10 '16 at 16:55
  • 2
    First you probably want to decode it into an array(For this see: http://stackoverflow.com/q/29308898/3933332). After that you want to filter your array, before you encode it again, and for this see: http://stackoverflow.com/q/1503579/3933332 – Rizier123 Aug 10 '16 at 16:55
  • @Rizier123 ok so first part is easy as `$json_array = json_decode($json,true); $json_data = $json_array["data"];` Could you please help me a bit on the next step? Can I use the JSON as a simple PHP array? Sorry but it's not that clear to me –  Aug 10 '16 at 17:12
  • 1
    @Alessandro First step is perfect! You decoded your json string to a php array. Now if you do: `print_r($json_array);` you actually see how the array structure looks like. And before you filter your array you want to either use a simple foreach loop or `array_map()` to divide each number of your subArray by 100. – Rizier123 Aug 10 '16 at 17:29
  • 1
    Now you want to filter the subArray with the key `data`. For this you want to look at the second linked Q&A from my above comment. And in pseudo code this is what you want to do next: `$json_array["data"] = filter($json_array["data"]);`(The filter function is of course just pseudo code, but it is important that you just want to filter your subArray). After that at the end you just want to encode the array again. – Rizier123 Aug 10 '16 at 17:29

2 Answers2

1

Well, I know this is not the best practice, but if it's as simple as this, you can do the following.

$json = '{"data":[0,55,78,-32,-46,37]}';

// decoding the string to objects & arrays
$x = json_decode($json);

// applying a function on each value of the array
$x->data = array_map(
               function($a)
               { 
                   if( $a >= 0 ) return $a/100; 
                   else return null; 
               }, 
               $x->data
            );
// Removing empty values of the array
$x->data = array_filter($x->data);

// making a JSON array
$jsonData = json_encode(array_values($x->data));

// inserting a JSON array in a JSON Object
$json = '{"data":' . $jsonData . '}';

// here is your {"data":[0,0.55,0.78,0.37]}
echo $json;

Hope it helps !

Btw, I had to trick the json encode with array_values to prevent the creation of an object rather than an array for the data content. But I guess there is a better method that I just don't know ...


EDIT :

Find out the way :D

Once empty values removed from the array, just do :

$x->data = array_values($x->data);
$json = json_encode($x);

This will do the trick and it will not create issues with the rest of the object.

Bobot
  • 1,118
  • 8
  • 19
  • How can I have this `$jsonData` inside a JSON like `{"sample_rate":44100,"samples_per_pixel":4410,"bits":8,"length":1847,"data":[$jsonData]` I'm asking because I tried `$json = json_encode($x);` but it's wrong. I need to keep all other arrays. Thanks for the help! –  Aug 10 '16 at 22:45
  • 1
    @Alessandro check out the EDIT :p – Bobot Aug 11 '16 at 09:01
  • Thanks but it's exactly what I tried before, the output is `{"sample_rate":44100,"samples_per_pixel":4410,"bits":8,"length":1847,"data":{"5":0.37,"7":0.17,...` and not `{"sample_rate":44100,"samples_per_pixel":4410,"bits":8,"length":1847,"data":{0.37,0.17,...` –  Aug 11 '16 at 09:09
  • 1
    Then you did not used array_values on it, I tried it and I have exactly this : `{"test":{"a": "b"}, "data":[0,0.55,0.78,0.37]}` ... can you give a link to your current code ? – Bobot Aug 11 '16 at 09:41
  • 1
    :) thanks, I forgot one line...`$x->data = array_filter($x->data); $x->data = array_values($x->data); $json = json_encode($x); ` –  Aug 11 '16 at 10:03
0

Alessandro:

Here's my approach, feel free to try. json_decode and a simple foreach can help you...

Code:

$json = array();
$result = array();

$json = '{"data":[0,55,78,-32,-46,37]}';

$decoded_json=json_decode($json, TRUE);

foreach ($decoded_json['data'] as &$value) {
    if ($value >= 0){
        $value = $value / 100;
        $result[]=$value;
    }

}

echo json_encode($result);

?>

Result:

[0,
0.55,
0.78,
0.37
]
Joel Hernandez
  • 2,195
  • 1
  • 13
  • 17