Skip to content
Advertisement

how to extract key and value from json content?

what I have tried is this.

public function toArrayss(){

        $json = '{"notificationType":"Bounce","bounce":{"feedbackId":"010201762c4054a2-424cab55-f6b7-420c-8f51-f1f13cb8b702-000000","bounceType":"Transient","bounceSubType":"General","bouncedRecipients":[{"emailAddress":"chintu@mailinatorc.com","action":"failed","status":"4.4.7","diagnosticCode":"smtp; 554 4.4.7 Message expired: unable to deliver in 840 minutes.<421 4.4.0 Unable to lookup DNS for mailinatorc.com>"}],"timestamp":"2020-12-04T05:36:22.000Z","reportingMTA":"dsn; a4-1.smtp-out.eu-west-1.amazonses.com"},"mail":{"timestamp":"2020-12-03T13:58:22.222Z","source":"ooionline@opsimplify.com","sourceArn":"arn:aws:ses:eu-west-1:128134649499:identity/opsimplify.com","sourceIp":"3.248.44.136","sendingAccountId":"128134649499","messageId":"0102017628e5910e-f8d2fba5-c25f-4109-8571-246ca3c983b2-000000","destination":["chintu@mailinatorc.com"],"headersTruncated":false,"headers":[{"name":"Received","value":"from sharabh.ooionline.com (ec2-3-248-44-136.eu-west-1.compute.amazonaws.com [3.248.44.136]) by email-smtp.amazonaws.com with SMTP (SimpleEmailService-d-8DMC1AGE7) id zcEZl2C63kMsDHYhlI2W for chintu@mailinatorc.com; Thu, 03 Dec 2020 13:58:22 +0000 (UTC)"},{"name":"Date","value":"Thu, 3 Dec 2020 14:58:22 +0100"},{"name":"To","value":"chintu@mailinatorc.com"},{"name":"From","value":"OPSimplify <ooionline@opsimplify.com>"},{"name":"Reply-To","value":"Email Notification <noreply@opsimplify.com>"},{"name":"Subject","value":"Votre accès à la plateforme collaborative SHARABH"},{"name":"Message-ID","value":"<PhTQOVF4ZuRS5icap8Bt6u6KCcBd3l2ihs2RPqYmpI@sharabh.ooionline.com>"},{"name":"X-Mailer","value":"PHPMailer 6.0.7 (https://github.com/PHPMailer/PHPMailer)"},{"name":"MIME-Version","value":"1.0"},{"name":"Content-Type","value":"text/html; charset=UTF-8"}],"commonHeaders":{"from":["OPSimplify <ooionline@opsimplify.com>"],"replyTo":["Email Notification <noreply@opsimplify.com>"],"date":"Thu, 3 Dec 2020 14:58:22 +0100","to":["chintu@mailinatorc.com"],"messageId":"<PhTQOVF4ZuRS5icap8Bt6u6KCcBd3l2ihs2RPqYmpI@sharabh.ooionline.com>","subject":"Votre accès à la plateforme collaborative SHARABH"}}}
';
        $objs = json_decode($json,true);

        $key = ['notificationType','bounceType','bounceSubType','timestamp'];
        foreach ($objs as $obj => $values )  {
            // echo $obj . ":". $values;
            dd($objs);
            // echo $values ;

        }

Can anyone help me in extracting keys and values like notificationType,bounceType,timestamp etc; It would be great help for me if anyone help me achieving this. Thank you.

Advertisement

Answer

json_decode decode a string converting it in php data structures: array or object. Check docs for json_decode.

You get object if you pass the second parameter as false:

<?php

$decoded = json_decode(..., false);

echo $decoded->notificationType;

And get associative array if pass true as the second parameter

<?php

$decoded = json_decode(..., true);

echo $decoded['notificationType'];

Your problem is that you are handling the returned array as a collection of notifications. Note that $objs only contains a single notification object.

<?php
function toArrayss() {
    
    $json = '{"notificationType":"Bounce","bounce":{"feedbackId":"010201762c4054a2-424cab55-f6b7-420c-8f51-f1f13cb8b702-000000","bounceType":"Transient","bounceSubType":"General","bouncedRecipients":[{"emailAddress":"chintu@mailinatorc.com","action":"failed","status":"4.4.7","diagnosticCode":"smtp; 554 4.4.7 Message expired: unable to deliver in 840 minutes.<421 4.4.0 Unable to lookup DNS for mailinatorc.com>"}],"timestamp":"2020-12-04T05:36:22.000Z","reportingMTA":"dsn; a4-1.smtp-out.eu-west-1.amazonses.com"},"mail":{"timestamp":"2020-12-03T13:58:22.222Z","source":"ooionline@opsimplify.com","sourceArn":"arn:aws:ses:eu-west-1:128134649499:identity/opsimplify.com","sourceIp":"3.248.44.136","sendingAccountId":"128134649499","messageId":"0102017628e5910e-f8d2fba5-c25f-4109-8571-246ca3c983b2-000000","destination":["chintu@mailinatorc.com"],"headersTruncated":false,"headers":[{"name":"Received","value":"from sharabh.ooionline.com (ec2-3-248-44-136.eu-west-1.compute.amazonaws.com [3.248.44.136]) by email-smtp.amazonaws.com with SMTP (SimpleEmailService-d-8DMC1AGE7) id zcEZl2C63kMsDHYhlI2W for chintu@mailinatorc.com; Thu, 03 Dec 2020 13:58:22 +0000 (UTC)"},{"name":"Date","value":"Thu, 3 Dec 2020 14:58:22 +0100"},{"name":"To","value":"chintu@mailinatorc.com"},{"name":"From","value":"OPSimplify <ooionline@opsimplify.com>"},{"name":"Reply-To","value":"Email Notification <noreply@opsimplify.com>"},{"name":"Subject","value":"Votre accès à la plateforme collaborative SHARABH"},{"name":"Message-ID","value":"<PhTQOVF4ZuRS5icap8Bt6u6KCcBd3l2ihs2RPqYmpI@sharabh.ooionline.com>"},{"name":"X-Mailer","value":"PHPMailer 6.0.7 (https://github.com/PHPMailer/PHPMailer)"},{"name":"MIME-Version","value":"1.0"},{"name":"Content-Type","value":"text/html; charset=UTF-8"}],"commonHeaders":{"from":["OPSimplify <ooionline@opsimplify.com>"],"replyTo":["Email Notification <noreply@opsimplify.com>"],"date":"Thu, 3 Dec 2020 14:58:22 +0100","to":["chintu@mailinatorc.com"],"messageId":"<PhTQOVF4ZuRS5icap8Bt6u6KCcBd3l2ihs2RPqYmpI@sharabh.ooionline.com>","subject":"Votre accès à la plateforme collaborative SHARABH"}}}
';
    $objs = json_decode($json, true);
    
    $key = ['notificationType', 'bounceType', 'bounceSubType', 'timestamp'];
    foreach ($objs as $obj => $values) {
        // echo $obj . ":". $values;
        dd($objs);
        // echo $values ;
        
    }
}

Modify your code like this

<?php

public function toArrays($json) {
    
    // if the json string contains a collection of notifications
    // this line returns an array that contains multiple notifications
    // in the other hand, if the json string contains a single notification object
    // this line returns a single notification as array
    return json_decode($json, true);
}

In this way you receive the json content as parameter and toArrays method only decode it and returns as array. You can iterate it with foreach, and can access to the indexes in the traditional way.

<?php

$asArray = $someObject->asArrays($someJsonString);

echo $asArray['some_property'];

foreach ($asArray as $key => $value) {
    echo ">" . $key;

    if (is_array($value)) {
        foreach ($value as $subKey => $subValue) {
            echo ">>>" . $subKey . "n";
            echo ">>>>" . $subValue . "n";
        }
    } else {
        echo ">>" . $value . "n";
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement