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?

5
  • 1
    php.net/manual/en/function.json-decode.php Commented Aug 10, 2016 at 16:55
  • 2
    First you probably want to decode it into an array(For this see: stackoverflow.com/q/29308898/3933332). After that you want to filter your array, before you encode it again, and for this see: stackoverflow.com/q/1503579/3933332 Commented Aug 10, 2016 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 Commented Aug 10, 2016 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. Commented Aug 10, 2016 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. Commented Aug 10, 2016 at 17:29

2 Answers 2

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.

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

5 Comments

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!
@Alessandro check out the EDIT :p
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,...
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 ?
:) thanks, I forgot one line...$x->data = array_filter($x->data); $x->data = array_values($x->data); $json = json_encode($x);
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
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.