Filter json data in php

Asked

Viewed 21 times

0

Boas, I would like to filter the date of a json file. The date I receive is something like this.

{
"max_new_quota": 21474836480,
"username": "[email protected]",
"rl": {
    "value": "60",
    "frame": "h"
},
"is_relayed": 0,
"name": "User Name",
"active": "✓",
"active_int": 1,
"domain": "domain.com",
"local_part": "username",
"quota": 3221225472,
"attributes": {
    "force_pw_update": "0",
    "tls_enforce_in": "1",
    "tls_enforce_out": "1",
    "sogo_access": "0",
    "mailbox_format": "maildir:",
    "quarantine_notification": "never"
},
"quota_used": 10448756,
"percent_in_use": 0,
"messages": 125,
"spam_aliases": 0,
"percent_class": "success"
}

I wanted to know how I can filter this in php to for example show the quota and the quota used and so on. Thank you

1 answer

0


It’s simple, use it json_decode and pass in the second parameter true to create a array associative of that , example:

<?php

$json = '{
"max_new_quota": 21474836480,
"username": "[email protected]",
"rl": {
    "value": "60",
    "frame": "h"
},
"is_relayed": 0,
"name": "User Name",
"active": "&#10003;",
"active_int": 1,
"domain": "domain.com",
"local_part": "username",
"quota": 3221225472,
"attributes": {
    "force_pw_update": "0",
    "tls_enforce_in": "1",
    "tls_enforce_out": "1",
    "sogo_access": "0",
    "mailbox_format": "maildir:",
    "quarantine_notification": "never"
},
"quota_used": 10448756,
"percent_in_use": 0,
"messages": 125,
"spam_aliases": 0,
"percent_class": "success"
}';

// criando o array associativo
$data = json_decode($json, true);

// imprimindo os dados
echo $data['quota']; // saída quota
echo PHP_EOL;
echo $data['quota_used']; // saída quota_used

and so on you can redeem all items if you wish.

  • Thank you so much for your comment already helped me a lot. I would just like to know how I could get the values within rl

  • @Josémartins $data['rl']['value'] and $data['rl']['frame']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.