Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Stripping [ ] from JSON arrays

9 views
Skip to first unread message

The Doctor

unread,
Jan 9, 2023, 8:17:55 PM1/9/23
to
This morning I got this from Moeris:


Here's an example working one:

{
"store_id":"store3",
"checkout_id":"chkt23NGFtore3",
"api_token":"yesguy",
"txn_total":"189.00",
"environment":"qa",
"action":"preload"
}

You repeated the same items section 3 times and your commas and braces are
incorrectly placed. Correct your preload, and let me know if you still get a
blank screen.

Follow the formatting shown here for the preload:
https://developer.moneris.com/sitecore/media%20library/Hidden/MCO/Preload%20Request?sc_lang=en


now given code processor



<?=session_start();
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Professional Development Solutions - Online Form</title>
<meta name="description" content="PD Solutions is an educational company that delivers on-line webinars and webcasts to various organizations or people" />
<meta name="robots" content="index, follow" />
<link rel="stylesheet" type="text/css" href="css/css.css"/>
<link rel="stylesheet" type="text/css" href="css/css2.css"/>
<link rel="stylesheet" href="css/uniform.default.css" type="text/css" media="screen">
<link rel="stylesheet" href="css/uniform.agent.css" type="text/css" media="screen">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">


</head>
<body>
<?php
if(!empty($_SESSION['sessiondata'])){
}
?>
<div id="wrapper">
<!--header begins-->
<?php
include("includes/header.php");
?>
<!--header ends-->
<FORM ACTION="https://gatewayt.moneris.com/chktv2/request/request.php" method=post>
<?php
$myObj = [];
$cartarray= [];
$itemssubarray= [];
$storevalues= [];
$storevalues2= [];
$contact_details=[];
$shipping_details=[];
$billing_details=[];
$arr2 =array();
?>
<center>
<!-- Store Settings-->
<!------- DEFINE CHARGE TOTAL HERE --->
<br/><br/><h3>Your Purchase Total Is: </h3> <br/> <h2>$<?=$_POST['charge_total']?></h2>
<!-- Unique Order ID -->
<!-- Additional Optional Details -->
<!-- Item Information -->
<?php

$items_count = 1;
$subtotal = 0;
$subtotala = 0;
$arr = array();
if (isset($_POST["submit"])){
foreach ($_POST['quantity'] as $key => $value) {

if( $value > 0){

if(isset($_POST['with_gst'][$key])){
$unit_cost = 63.00;
}else{
$unit_cost = 60.00; //no gst included
}

$subtotal = ($unit_cost * $value);
$quantity = ($value);

$itemssubarray[] = array(
"items"=>array(
'url' => "https://www.pdsolutions.ca/images/procircle.png",
'description' => $_POST['description'][$key],
'product_code' => $_POST['id'][$key],
'unit_cost' => $unit_cost,
'quantity' => $quantity,
),
);
}

$subtotala += ($unit_cost * $value);
$items_count++;
}
$cartarray[] = array ("cart"=>array($itemssubarray));

// array_push($cartarray,$itemssubasrray);

$cartarray[] = array(
'subtotal' => $subtotala,
"tax"=>array(
'amount' => ($subtotala * 0.05),
'description' => "GST",
'rate' => "0.05",
),
);
array_push($arr, $cartarray);
}

?>
<?php

if (isset($_POST["submit"])){
(array_push($storevalues,[
//'sessValue' => $_POST['sessionid1'],
'username'=> "demouser",
'password' => "password",
'store_id' => "store3",
'checkout_id' => "chkt23NGFtore3",
'api_token' => "yesguy",
'txn_total' => $_POST['charge_total'],
'environment' => "qa",
'action' => "preload",
]));

$contact_details[] = array(
"contact_details"=>array(
'first_name' =>$_POST['bill_first_name'],
'last_name' => $_POST['bill_last_name'],
'email' => $_POST['email'],
'phone' => $_POST['bill_phone'],
)
);
array_push($arr2, $contact_details);

$billing_details[] = array (
"billing_details"=>array(
'address_1' => $_POST['bill_address_one'],
'city' => $_POST['bill_city'],
'province' => $_POST['bill_state_or_province'],
'country' => "CA",
'postal_code' => $_POST['bill_postal_code'],

)
);

?>
<?php
$myObj = array_merge($storevalues,$cartarray,$contact_details,$billing_details);
//$storevalues2 = substr($storevalues,1,strlen($storevalues) -2);
//$myObj = $storevalues;
$myJSON = json_encode($myObj);
echo $myJSON;
}

?>
<div id="outerDiv" style="width:400px"; height"300px">`
<div id="monerisCheckout">
</div>

</div>
<script src="https://gatewayt.moneris.com/chktv2/js/chkt_v2.00.js" async></script>

<script>
var myCheckout = new monerisCheckout();
myCheckout.setMode("qa");
myCheckout.setCheckoutDiv("monerisCheckout");
monerisCheckout.startCheckout('<?php echo $token ?>');

var myPageLoad = function(data) {
console.log(data);
const obj = JSON.parse(data);
console.log(obj.ticket);

};

var myCancelTransaction = function(data) {
console.log(data);
const obj = JSON.parse(data);
console.log(obj.ticket);
};

var myErrorEvent = function(data) {
console.log(data);
const obj = JSON.parse(data);
console.log(obj.ticket);
};

var myPaymentReceipt = function(data) {
console.log(data);
const obj = JSON.parse(data);
console.log(obj.ticket);
};

var myPaymentComplete = function(data) {
console.log(data);
const obj = JSON.parse(data);
console.log(obj.ticket);
};

/**
* Set callbacks in JavaScript:
*/
myCheckout.setCallback("page_loaded",myPageLoad);
myCheckout.setCallback("cancel_transaction",myCancelTransaction);
myCheckout.setCallback("error_event", myErrorEvent);
myCheckout.setCallback("payment_receipt",myPaymentReceipt);
myCheckout.setCallback("payment_complete", myPaymentComplete);
</script>
<INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Click to proceed to Secure Page">
</center>
</FORM>

<?php
include("includes/footer.php");
?>
</div>
</body>
</html>


This picks up data from a previous form.

Here is and example of what is generated:

[{"username":"demouser","password":"password","store_id":"store3","checkout_id":"chkt23NGFtore3","api_token":"yesguy","txn_total":"189.00","environment":"qa","action":"preload"},{"cart":[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Protecting Pollinators","product_code":"PP-1294","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Agricultural Health Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Pesticide Applicator Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"}}]]},{"subtotal":180,"tax":{"amount":9,"description":"GST","rate":"0.05"}},{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"ro...@nk.ca","phone":"7804734587"}},{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]

And here is what is being looked for

{
"store_id":"moneris",
"api_token":"hurgle",
"checkout_id":"chkt5BF66neris",
"txn_total":"452.00",
"environment":"qa",
"action":"preload",
"token": [
{
"data_key": "abc123datakey1",
"issuer_id": "645sddfvdrt4tefd"
},
{
"data_key": "abc123datakey2",
"issuer_id": "645sddfvdrt4tefd"
},
{
"data_key": "abc123datakey3",
"issuer_id": "645sddfvdrt4tefd"
}
],
"ask_cvv":"Y"
"order_no":"",
"cust_id":"chkt - cust - 0303",
"dynamic_descriptor":"dyndesc",
"language":"en",
"recur":{
"bill_now":"true",
"recur_amount":"1.00",
"start_date":"2021-11-21",
"recur_unit":"month",
"recur_period":"1",
"number_of_recurs":"10"
},
"cart":{
"items":[
{
"url":"https:\/\/example.com\/examples\/item1.jpg",
"description":"One item",
"product_code":"one_item",
"unit_cost":"100.00",
"quantity":"1"
},
{
"url":"https:\/\/example.com\/examples\/item2.jpg",
"description":"Two item",
"product_code":"two_item",
"unit_cost":"200.00",
"quantity":"1"
},
{
"url":"https:\/\/example.com\/examples\/item3.jpg",
"description":"Three item",
"product_code":"three_item",
"unit_cost":"100.00",
"quantity":"1"
}
],
"subtotal":"400.00",
"tax":{
"amount":"52.00",
"description":"Taxes",
"rate":"13.00"
}
},
"contact_details":{
"first_name":"bill",
"last_name":"smith",
"email":"te...@moneris.com",
"phone":"4165551234"
},
"shipping_details":{
"address_1":"1 main st",
"address_2":"Unit 2012",
"city":"Toronto",
"province":"ON",
"country":"CA",
"postal_code":"M1M1M1"
},
"billing_details":{
"address_1":"1 main st",
"address_2":"Unit 2000",
"city":"Toronto",
"province":"ON",
"country":"CA",
"postal_code":"M1M1M1"
}
}

How do I strip unnecessary [] ?
--
Member - Liberal International This is doc...@nk.ca Ici doc...@nk.ca
Yahweh, King & country!Never Satan President Republic!Beware AntiChrist rising!
Look at Psalms 14 and 53 on Atheism https://www.empire.kred/ROOTNK?t=94a1f39b
Birthdate: 29 Jan 1969 Redhill, Surrey, England Beware https://mindspring.com

JJ

unread,
Jan 9, 2023, 11:29:21 PM1/9/23
to
On Tue, 10 Jan 2023 01:17:47 -0000 (UTC), The Doctor wrote:
>
> How do I strip unnecessary [] ?

I'd use array's `reduce()` for that. e.g.

var resultObj = theArray.reduce((res, obj) => Object.assign(res, obj), {});

The Doctor

unread,
Jan 10, 2023, 1:03:23 AM1/10/23
to
In article <1rb3ua6kn88tn.o...@40tude.net>,
Will try!

Stefan Froehlich

unread,
Jan 10, 2023, 3:10:19 AM1/10/23
to
In comp.lang.php The Doctor <doc...@doctor.nl2k.ab.ca> wrote:
> $contact_details=[];
[...]
> $contact_details[] = array(
> "contact_details"=>array(
> 'first_name' =>$_POST['bill_first_name'],
> 'last_name' => $_POST['bill_last_name'],
> 'email' => $_POST['email'],
> 'phone' => $_POST['bill_phone'],
> )
> );
[...]
> $myObj = array_merge($storevalues,$cartarray,$contact_details,$billing_details);
> $myJSON = json_encode($myObj);

> Here is and example of what is generated:

> [{"username":"demouser","password":"password","store_id":"store3","checkout_id":"chkt23NGFtore3","api_token":"yesguy","txn_total":"189.00","environment":"qa","action":"preload"},{"cart":[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Protecting Pollinators","product_code":"PP-1294","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Agricultural Health Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/procircle.png","description":"Pesticide Applicator Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"}}]]},{"subtotal":180,"tax":{"amount":9,"description":"GST","rate":"0.05"}},{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"ro...@nk.ca","phone":"7804734587"}},{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]

> And here is what is being looked for
>
> {
> "store_id":"moneris",
> [...]
> }

> How do I strip unnecessary [] ?

First of all, you should strip unnecessary information from your
postings - in your case the content of $myObj and the output are the
only necessary information.

#v+
$a = array();
$b = array();
$a[] = ['contact_details' => ['y' => 'z']];
$b[] = ['billing_details' => ['y' => 'z']];
$c = array_merge($a, $b);
var_dump(json_encode($c));
#v-

string(59) "[{"contact_details":{"y":"z"}},{"billing_details":{"y":"z"}}]"


#v+
$a = ['contact_details' => ['y' => 'z']];
$b = ['billing_details' => ['y' => 'z']];
$c = array_merge($a, $b);
var_dump(json_encode($c));
#v-

string(55) "{"contact_details":{"y":"z"},"billing_details":{"y":"z"}}"

So there is no need to use reduce() or anything else. You are simply
using one (non-associative) level of arrays too much. This would be
easier to debug if you add a print_r($myObj); to your debugging
output.


Personally I'd prefer another version without the irritating
array_merge for better readability, but this is just a matter of
taste producing the same array and the same output:

#v+
$a = ['y' => 'z'];
$b = ['y' => 'z'];
$c = ['contact_details' => $a, 'billing_details' => $b];
var_dump(json_encode($c));
#v-

Bye,
Stefan

--
http://kontaktinser.at/ - die kostenlose Kontaktboerse fuer Oesterreich
Offizieller Erstbesucher(TM) von mmeike

Stefan: die süße Verführung!
(Sloganizer)

Elhwen Dico

unread,
Jan 11, 2023, 11:09:35 AM1/11/23
to
const array = [
[0, 1, 2], [], ["tutu"], undefined
];

const result = array.filter(
(tab) => (tab && tab.length));

console.log(result);

-> [ [ 0, 1, 2 ], [ 'tutu' ] ]

you could improve filter callback to check that array members are arrays.

The Doctor

unread,
Jan 11, 2023, 11:18:36 AM1/11/23
to
In article <63bedf36$0$3100$426a...@news.free.fr>,
giving an update, I finally found someone at Moneris to work with.

I hate bad doumentation.
0 new messages