Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #19286 > unrolled thread

Stripping a JSON of extra characters in PHP

Started bydoctor@doctor.nl2k.ab.ca (The Doctor)
First post2023-02-01 15:45 +0000
Last post2023-02-07 16:05 +0000
Articles 20 on this page of 31 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-01 15:45 +0000
    Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-01 16:09 +0000
      Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-03 18:12 +0100
        Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-03 23:56 +0000
          Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-05 00:28 +0100
            Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-05 00:06 +0000
              Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-05 14:17 +0100
                Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-05 20:58 +0000
                  Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-05 23:38 +0100
      Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-04 11:23 +0100
        Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-05 00:30 +0100
        Re: Stripping a JSON of extra characters in PHP Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-02-04 23:44 +0000
    Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-05 16:14 +0100
      Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-06 01:31 +0000
        Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-06 08:14 +0100
          Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-06 15:28 +0000
            Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-06 18:14 +0100
              Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 00:56 +0000
            Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-06 20:40 +0100
              Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 00:57 +0000
                Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 01:28 +0000
                  Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-07 09:16 +0100
                    Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-07 09:31 +0100
                      Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 16:10 +0000
                        Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-09 20:22 +0100
                          Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-09 21:01 +0000
                            Re: Stripping a JSON of extra characters in PHP "J.O. Aho" <user@example.net> - 2023-02-10 11:49 +0100
                              Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-10 14:39 +0000
                    Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 16:05 +0000
                Re: Stripping a JSON of extra characters in PHP Arno Welzel <usenet@arnowelzel.de> - 2023-02-07 09:13 +0100
                  Re: Stripping a JSON of extra characters in PHP doctor@doctor.nl2k.ab.ca (The Doctor) - 2023-02-07 16:05 +0000

Page 1 of 2  [1] 2  Next page →


#19286 — Stripping a JSON of extra characters in PHP

Fromdoctor@doctor.nl2k.ab.ca (The Doctor)
Date2023-02-01 15:45 +0000
SubjectStripping a JSON of extra characters in PHP
Message-ID<tre1ei$2lti$75@gallifrey.nk.ca>
From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet

I need a JSON as follows:

{
	"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":"test@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"
																																																																																											    }
																																																																																											    }











But in testng I am getting



[{"store_id":"store3","api_token":"yesguy","checkout_id":"chkt23NGFtore3","txn_total":"126.00","environment":"qa","action":"preload"},[{"cart":[[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Agricultural Health Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Pesticide Applicator Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}}]]]}],[{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"dog@nk.ca","phone":"7809999999"}}],[{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]]

               
	       How can I carefully strip unnecessary punctuation?
-- 
Member - Liberal International This is doctor@nk.ca Ici doctor@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 
When they offer free stuff, demand to know how much it will cost. -unknown Beware https://mindspring.com

[toc] | [next] | [standalone]


#19287

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-02-01 16:09 +0000
Message-ID<875yclmkpv.fsf@bsb.me.uk>
In reply to#19286
doctor@doctor.nl2k.ab.ca (The Doctor) writes:

> From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
>
> I need a JSON as follows:
>
> {
> 	"store_id":"moneris",
> 	"api_token":"hurgle",

What you pasted as what you "need" is not even valid JSON and has a huge
number of pointless spaces in it making it almost unreadable (in my news
client).

> But in testng I am getting
>
> [{"store_id":"store3","api_token":"yesguy","checkout_id":"chkt23NGFtore3","txn_total":"126.00","environment":"qa","action":"preload"},[{"cart":[[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Agricultural Health Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Pesticide Applicator Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}}]]]}],[{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"dog@nk.ca","phone":"7809999999"}}],[{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]]
>
> 	       How can I carefully strip unnecessary punctuation?

But that one is (a) valid and (b) has no  unnecessary punctuation, so I
don't know what it is you mean.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#19288

FromArno Welzel <usenet@arnowelzel.de>
Date2023-02-03 18:12 +0100
Message-ID<k44tjfFku3dU3@mid.individual.net>
In reply to#19287
Ben Bacarisse, 2023-02-01 17:09:

> doctor@doctor.nl2k.ab.ca (The Doctor) writes:
> 
>> From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
>>
>> I need a JSON as follows:
>>
>> {
>> 	"store_id":"moneris",
>> 	"api_token":"hurgle",
> 
> What you pasted as what you "need" is not even valid JSON and has a huge
> number of pointless spaces in it making it almost unreadable (in my news
> client).

First of all - to all of you: learn how to use validators and
beautifiers. It helps a lot to understand the structure and differences
between multiple JSON files.

In the original sample was only one single comma missing. Otherwise it
is ok:

{
   "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":"test@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"
   }
}

>> But in testng I am getting
>>
>> [{"store_id":"store3","api_token":"yesguy","checkout_id":"chkt23NGFtore3","txn_total":"126.00","environment":"qa","action":"preload"},[{"cart":[[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Agricultural Health Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Pesticide Applicator Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}}]]]}],[{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"dog@nk.ca","phone":"7809999999"}}],[{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]]
>>
>> 	       How can I carefully strip unnecessary punctuation?
> 
> But that one is (a) valid and (b) has no  unnecessary punctuation, so I
> don't know what it is you mean.

The structure is completely different and not just with "unnecessary
punctuation" - there is no simple way to "strip" anything, it has to be
completely parsed and output again in the desired way:

[
   {
      "store_id":"store3",
      "api_token":"yesguy",
      "checkout_id":"chkt23NGFtore3",
      "txn_total":"126.00",
      "environment":"qa",
      "action":"preload"
   },
   [
      {
         "cart":[
            [
               [
                  {
                     "items":{
                        "url":
    "https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png",
                        "description":"Agricultural Health Study",
                        "product_code":"AHS-1298",
                        "unit_cost":60,
                        "quantity":"1"
                     },
                     "subtotal":60,
                     "tax":{
                        "amount":3,
                        "description":"GST",
                        "rate":"0.05"
                     }
                  },
                  {
                     "items":{
                        "url":
    "https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png",
                        "description":"Pesticide Applicator Records",
                        "product_code":"PAR-1302",
                        "unit_cost":60,
                        "quantity":"1"
                     },
                     "subtotal":60,
                     "tax":{
                        "amount":3,
                        "description":"GST",
                        "rate":"0.05"
                     }
                  }
               ]
            ]
         ]
      }
   ],
   [
      {
         "contact_details":{
            "first_name":"Dave",
            "last_name":"Yadallee",
            "email":"dog@nk.ca",
            "phone":"7809999999"
         }
      }
   ],
   [
      {
         "billing_details":{
            "address_1":"",
            "city":"",
            "province":"",
            "country":"CA",
            "postal_code":""
         }
      }
   ]
]



-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#19289

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-02-03 23:56 +0000
Message-ID<87ilgis3p2.fsf@bsb.me.uk>
In reply to#19288
Arno Welzel <usenet@arnowelzel.de> writes:

> Ben Bacarisse, 2023-02-01 17:09:
>
>> doctor@doctor.nl2k.ab.ca (The Doctor) writes:
>> 
>>> From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
>>>
>>> I need a JSON as follows:
>>>
>>> {
>>> 	"store_id":"moneris",
>>> 	"api_token":"hurgle",
>> 
>> What you pasted as what you "need" is not even valid JSON and has a huge
>> number of pointless spaces in it making it almost unreadable (in my news
>> client).
>
> First of all - to all of you: learn how to use validators and
> beautifiers.

You think I don't?  Now that you've got that off your chest, what's the
second thing we -- all of us -- need to learn? ;-)

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#19292

FromArno Welzel <usenet@arnowelzel.de>
Date2023-02-05 00:28 +0100
Message-ID<k4881fF6dctU1@mid.individual.net>
In reply to#19289
Ben Bacarisse, 2023-02-04 00:56:

> Arno Welzel <usenet@arnowelzel.de> writes:
> 
>> Ben Bacarisse, 2023-02-01 17:09:
>>
>>> doctor@doctor.nl2k.ab.ca (The Doctor) writes:
>>>
>>>> From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
>>>>
>>>> I need a JSON as follows:
>>>>
>>>> {
>>>> 	"store_id":"moneris",
>>>> 	"api_token":"hurgle",
>>>
>>> What you pasted as what you "need" is not even valid JSON and has a huge
>>> number of pointless spaces in it making it almost unreadable (in my news
>>> client).
>>
>> First of all - to all of you: learn how to use validators and
>> beautifiers.
> 
> You think I don't?  Now that you've got that off your chest, what's the
> second thing we -- all of us -- need to learn? ;-)

What I told you. Then you could have seen, that the invalid example only
missed a comma.


-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#19295

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-02-05 00:06 +0000
Message-ID<87edr5q8lu.fsf@bsb.me.uk>
In reply to#19292
Arno Welzel <usenet@arnowelzel.de> writes:

> Ben Bacarisse, 2023-02-04 00:56:
>
>> Arno Welzel <usenet@arnowelzel.de> writes:
>> 
>>> Ben Bacarisse, 2023-02-01 17:09:
>>>
>>>> doctor@doctor.nl2k.ab.ca (The Doctor) writes:
>>>>
>>>>> From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
>>>>>
>>>>> I need a JSON as follows:
>>>>>
>>>>> {
>>>>> 	"store_id":"moneris",
>>>>> 	"api_token":"hurgle",
>>>>
>>>> What you pasted as what you "need" is not even valid JSON and has a huge
>>>> number of pointless spaces in it making it almost unreadable (in my news
>>>> client).
>>>
>>> First of all - to all of you: learn how to use validators and
>>> beautifiers.
>> 
>> You think I don't?  Now that you've got that off your chest, what's the
>> second thing we -- all of us -- need to learn? ;-)
>
> What I told you.

What a relief to know that you'd already imparted the first /and second
thing/ we all need to learn.  I feared I would not have the time to
learn enough to keep posting!

> Then you could have seen, that the invalid example only
> missed a comma.

Yes, I spotted that too.  You'll never guess how...

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#19296

FromArno Welzel <usenet@arnowelzel.de>
Date2023-02-05 14:17 +0100
Message-ID<k49ojdFdcn7U1@mid.individual.net>
In reply to#19295
Ben Bacarisse, 2023-02-05 01:06:

> Arno Welzel <usenet@arnowelzel.de> writes:
[...]
>> Then you could have seen, that the invalid example only
>> missed a comma.
> 
> Yes, I spotted that too.  You'll never guess how...

Well, then I wonder why you just wrote this:

"But that one is (a) valid and (b) has no  unnecessary punctuation, so I
don't know what it is you mean."


-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#19298

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-02-05 20:58 +0000
Message-ID<87a61rq171.fsf@bsb.me.uk>
In reply to#19296
Arno Welzel <usenet@arnowelzel.de> writes:

> Ben Bacarisse, 2023-02-05 01:06:
>
>> Arno Welzel <usenet@arnowelzel.de> writes:
> [...]
>>> Then you could have seen, that the invalid example only
>>> missed a comma.
>> 
>> Yes, I spotted that too.  You'll never guess how...
>
> Well, then I wonder why you just wrote this:
>
> "But that one is (a) valid and (b) has no  unnecessary punctuation, so I
> don't know what it is you mean."

Because "that one" (the second sample shown) was (a) valid and (b) had
no unnecessary punctuation.  As result, I could not tell what the OP
meant by their question.  What's bothering you about this?

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#19299

FromArno Welzel <usenet@arnowelzel.de>
Date2023-02-05 23:38 +0100
Message-ID<k4apg1FibpsU1@mid.individual.net>
In reply to#19298
Ben Bacarisse, 2023-02-05 21:58:

> Arno Welzel <usenet@arnowelzel.de> writes:
> 
>> Ben Bacarisse, 2023-02-05 01:06:
>>
>>> Arno Welzel <usenet@arnowelzel.de> writes:
>> [...]
>>>> Then you could have seen, that the invalid example only
>>>> missed a comma.
>>>
>>> Yes, I spotted that too.  You'll never guess how...
>>
>> Well, then I wonder why you just wrote this:
>>
>> "But that one is (a) valid and (b) has no  unnecessary punctuation, so I
>> don't know what it is you mean."
> 
> Because "that one" (the second sample shown) was (a) valid and (b) had
> no unnecessary punctuation.  As result, I could not tell what the OP
> meant by their question.  What's bothering you about this?

Just read my other posts about this, maybe you get it. If not, it's ok
as well.

-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#19290

From"J.O. Aho" <user@example.net>
Date2023-02-04 11:23 +0100
Message-ID<k46q0rF1ne9U4@mid.individual.net>
In reply to#19287
On 01/02/2023 17:09, Ben Bacarisse wrote:
> doctor@doctor.nl2k.ab.ca (The Doctor) writes:
> 
>>  From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
>>
>> I need a JSON as follows:
>>
>> {
>> 	"store_id":"moneris",
>> 	"api_token":"hurgle",
> 
> What you pasted as what you "need" is not even valid JSON and has a huge
> number of pointless spaces in it making it almost unreadable (in my news
> client). 
> I don't know what it is you mean.
As I saw it, the OP post was more about the formatting of the json that 
is generated than about if his json was correct or not, he wants a multi 
line json with indentations instead of a single line compressed json.

-- 
  //Aho

[toc] | [prev] | [next] | [standalone]


#19293

FromArno Welzel <usenet@arnowelzel.de>
Date2023-02-05 00:30 +0100
Message-ID<k48847F6dctU2@mid.individual.net>
In reply to#19290
J.O. Aho, 2023-02-04 11:23:

> On 01/02/2023 17:09, Ben Bacarisse wrote:
>> doctor@doctor.nl2k.ab.ca (The Doctor) writes:
>>
>>>  From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
>>>
>>> I need a JSON as follows:
>>>
>>> {
>>> 	"store_id":"moneris",
>>> 	"api_token":"hurgle",
>>
>> What you pasted as what you "need" is not even valid JSON and has a huge
>> number of pointless spaces in it making it almost unreadable (in my news
>> client). 
>> I don't know what it is you mean.
> As I saw it, the OP post was more about the formatting of the json that 
> is generated than about if his json was correct or not, he wants a multi 
> line json with indentations instead of a single line compressed json.

The OP showed to examples of *similar* JSON data with many identical
properties, but in a slightly different structure. So I am not sure if
the question was onloy about formatting - but only the OP can tell.

-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#19294

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2023-02-04 23:44 +0000
Message-ID<87k00xq9l8.fsf@bsb.me.uk>
In reply to#19290
"J.O. Aho" <user@example.net> writes:

> On 01/02/2023 17:09, Ben Bacarisse wrote:
>> doctor@doctor.nl2k.ab.ca (The Doctor) writes:
>> 
>>>  From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
>>>
>>> I need a JSON as follows:
>>>
>>> {
>>> 	"store_id":"moneris",
>>> 	"api_token":"hurgle",
>> What you pasted as what you "need" is not even valid JSON and has a huge
>> number of pointless spaces in it making it almost unreadable (in my news
>> client). I don't know what it is you mean.
>
> As I saw it, the OP post was more about the formatting of the json
> that is generated than about if his json was correct or not, he wants
> a multi line json with indentations instead of a single line
> compressed json.

Weird.  I didn't interpret a desire to "strip unnecessary punctuation"
as a formatting question, but I suppose it could be: unnecessary for
display even though necessary for valid syntax.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#19297

From"J.O. Aho" <user@example.net>
Date2023-02-05 16:14 +0100
Message-ID<k49veoF1ne8U3@mid.individual.net>
In reply to#19286
On 01/02/2023 16:45, The Doctor wrote:
>  From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
> 
> I need a JSON as follows:
> 
> {
> 	"store_id":"moneris",
> 	"api_token":"hurgle",

For console output:
echo json_encode($object, JSON_PRETTY_PRINT);

For html output:
<pre>
<?php echo nl2br(json_encode($yourArrayOrObject, JSON_PRETTY_PRINT)); ?>
</pre>

If you have an already json encoded string, then you need to use 
json_decode to get an object and then encode the object to prettify the 
json.


> [{"store_id":"store3","api_token":"yesguy","checkout_id":"chkt23NGFtore3","txn_total":"126.00","environment":"qa","action":"preload"},[{"cart":[[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Agricultural Health Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Pesticide Applicator Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}}]]]}],[{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"dog@nk.ca","phone":"7809999999"}}],[{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]]
>                  
> 	       How can I carefully strip unnecessary punctuation?

The unnecessary [] that you get is for you use an array to store things 
inside, use a class object instead.

Here is a small example, where only the cart is an array in the json:

--- start of php ---
$a = new class{};
$a->store_id = "123";
$a->api_token = "432";
$a->cart = array();
// add item to cart that cast an array to object
$a->cart[] = (object)[ "value" => "12.01", "item" => "banana" ];
// add item to cat that is an undefined object
$itemObj = new class{};
$itemObj->value = "10.02";
$itemObj->item = "apple";
$a->cart[] = $itemObj;
echo json_encode($a, JSON_PRETTY_PRINT);
--- eof ---

Only use array when you are to have a collection of items, for example a 
shopping cart, but do not use array when you have a single item, then 
better to use an object of some sort, for your example I would actually 
create classes that describes the json, you will of course have nested 
objects in the end.

-- 
  //Aho

[toc] | [prev] | [next] | [standalone]


#19300

Fromdoctor@doctor.nl2k.ab.ca (The Doctor)
Date2023-02-06 01:31 +0000
Message-ID<trpl8k$12si$72@gallifrey.nk.ca>
In reply to#19297
In article <k49veoF1ne8U3@mid.individual.net>,
J.O. Aho <user@example.net> wrote:
>On 01/02/2023 16:45, The Doctor wrote:
>>  From https://developer.moneris.com/livedemo/checkout/preload_req/guide/dotnet
>> 
>> I need a JSON as follows:
>> 
>> {
>> 	"store_id":"moneris",
>> 	"api_token":"hurgle",
>
>For console output:
>echo json_encode($object, JSON_PRETTY_PRINT);
>
>For html output:
><pre>
><?php echo nl2br(json_encode($yourArrayOrObject, JSON_PRETTY_PRINT)); ?>
></pre>
>
>If you have an already json encoded string, then you need to use 
>json_decode to get an object and then encode the object to prettify the 
>json.
>
>
>>
>[{"store_id":"store3","api_token":"yesguy","checkout_id":"chkt23NGFtore3","txn_total":"126.00","environment":"qa","action":"preload"},[{"cart":[[[{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Agricultural Health
>Study","product_code":"AHS-1298","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}},{"items":{"url":"https:\/\/www.pdsolutions.ca\/images\/newwhiteheader.png","description":"Pesticide
>Applicator
>Records","product_code":"PAR-1302","unit_cost":60,"quantity":"1"},"subtotal":60,"tax":{"amount":3,"description":"GST","rate":"0.05"}}]]]}],[{"contact_details":{"first_name":"Dave","last_name":"Yadallee","email":"dog@nk.ca","phone":"7809999999"}}],[{"billing_details":{"address_1":"","city":"","province":"","country":"CA","postal_code":""}}]]
>>                  
>> 	       How can I carefully strip unnecessary punctuation?
>
>The unnecessary [] that you get is for you use an array to store things 
>inside, use a class object instead.
>
>Here is a small example, where only the cart is an array in the json:
>
>--- start of php ---
>$a = new class{};
>$a->store_id = "123";
>$a->api_token = "432";
>$a->cart = array();
>// add item to cart that cast an array to object
>$a->cart[] = (object)[ "value" => "12.01", "item" => "banana" ];
>// add item to cat that is an undefined object
>$itemObj = new class{};
>$itemObj->value = "10.02";
>$itemObj->item = "apple";
>$a->cart[] = $itemObj;
>echo json_encode($a, JSON_PRETTY_PRINT);
>--- eof ---
>
>Only use array when you are to have a collection of items, for example a 
>shopping cart, but do not use array when you have a single item, then 
>better to use an object of some sort, for your example I would actually 
>create classes that describes the json, you will of course have nested 
>objects in the end.
>
>-- 

And in the case of recurring items when being puleld from 
a form getting parsed?

>  //Aho
>
>


-- 
Member - Liberal International This is doctor@nk.ca Ici doctor@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 
If their only strength is in enforcing weakness, then they will preside over ruin. -unknown Beware https://mindspring.com

[toc] | [prev] | [next] | [standalone]


#19301

From"J.O. Aho" <user@example.net>
Date2023-02-06 08:14 +0100
Message-ID<k4bnmkFffdeU1@mid.individual.net>
In reply to#19300
On 06/02/2023 02:31, The Doctor wrote:
> And in the case of recurring items when being puleld from
> a form getting parsed?

If you are thinking of post value which is an array, then the simplest 
would be:
$a->cart = (object)$_POST['cart']; // this may give you object you don't 
want

Just keep in mind that the data is unsafe to use, you should do the long 
way and validate the data and as you validate you can then add the 
values to a temp variable and if all the cart items passes validation, 
then add it to the main json object  (in this silly example the $a).

If you get a new cart posted (updates), then  replace the current one 
you have in the session, don't forget to validate the data before you 
replace the cart section.  Only have the latest validated one
$a->cart = $validated_shopping_cart; // validated_shopping_cart is a 
json object

for duplicate entries you have to decide what to do, cast an error or 
merge them together, that is up to you, but do not have duplicate items 
in your json object.

I hope that was the answer you was looking for.

-- 
  //Aho

[toc] | [prev] | [next] | [standalone]


#19302

Fromdoctor@doctor.nl2k.ab.ca (The Doctor)
Date2023-02-06 15:28 +0000
Message-ID<trr6aj$1v6p$58@gallifrey.nk.ca>
In reply to#19301
In article <k4bnmkFffdeU1@mid.individual.net>,
J.O. Aho <user@example.net> wrote:
>On 06/02/2023 02:31, The Doctor wrote:
>> And in the case of recurring items when being puleld from
>> a form getting parsed?
>
>If you are thinking of post value which is an array, then the simplest 
>would be:
>$a->cart = (object)$_POST['cart']; // this may give you object you don't 
>want
>
>Just keep in mind that the data is unsafe to use, you should do the long 
>way and validate the data and as you validate you can then add the 
>values to a temp variable and if all the cart items passes validation, 
>then add it to the main json object  (in this silly example the $a).
>
>If you get a new cart posted (updates), then  replace the current one 
>you have in the session, don't forget to validate the data before you 
>replace the cart section.  Only have the latest validated one
>$a->cart = $validated_shopping_cart; // validated_shopping_cart is a 
>json object
>
>for duplicate entries you have to decide what to do, cast an error or 
>merge them together, that is up to you, but do not have duplicate items 
>in your json object.
>
>I hope that was the answer you was looking for.
>

The cart is being derived from a previous form being passed into PHP!


Just an update


<?=session_start();
error_reporting(E_ALL);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
</head>
<?php 
 if(!empty($_SESSION['sessiondata'])){
 }
?>
<?php 
$myObj = [];
$cart = [];
$items = [];
$itemssubarray = [];
$storevalues= [];
$contact_details=[];
$shipping_details=[];
$billing_details=[];
$arr2 =array();

$items_count = 0;
$newtotal = 0;
$subtotal = 0;
$arr = array();
if (isset($_POST["submit"])){

$arr=array(
'store_id' => "store3",
'api_token' => "yesguy",
'checkout_id' => "chktGLFZQtore3",
'txn_total' => $_POST['charge_total'],
'environment' => "qa",
'action' => "preload",
'cart' => array( 
(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);

	$cart['items'][] =array(
         'url' => "https://www.pdsolutions.ca/images/newwhiteheader.png",
        'description' => $_POST['description'][$key],
        'product_code' => $_POST['id'][$key],
       'unit_cost' => $unit_cost,
       'quantity' => $quantity,
	),
    $newtotal += $subtotal;
    $items_count++;
    array_push(items,itemsubarray);
  	}
  }
         
        'subtotal' => $newtotal,
    if(isset($_POST['with_gst'][$key])){
        'tax'=>array(
          'amount' => ($newtotal * 0.05),
          'description' => "GST",
	  'rate' => "5.00",
          ),
    })
),
'contact_details'=>array(
	'first_name' =>$_POST['bill_first_name'],
	'last_name' => $_POST['bill_last_name'],
	'email' => $_POST['email'],
	'phone' => $_POST['bill_phone'],
	    ),
'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'],
	),

);

}


//	$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'],

//		)



$myObj = $arr;
$myJSON =  json_encode($myObj);

echo $myJSON;

// Prepare new cURL resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gatewayt.moneris.com/chkt/request/request.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myJSON);
 
$array_length = count($arr, COUNT_RECURSIVE);
 
// Set HTTP Header for POST request 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json' ,'Content-Length: ' . strlen($myJSON) 
 )
);
  
// Submit the POST request
$result = curl_exec($ch);
 
// Close cURL session handle
curl_close($ch);
print_r($arr);
print_r($result);
$response_data = json_decode($result);
print_r($response_data);
 
 
$result_data = json_decode($result, true);
print_r($result_data);
 
if( isset($result_data['response']['error']) &&  ( ! (bool) $result_data['response']['success'] ) ){
    echo 'error message';
}
$token = $result_data['response']['ticket'];
?>

<body>
                <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"></script>
   <script>
                let myCheckout = new monerisCheckout();
                myCheckout.setMode("qa");
                myCheckout.setCheckoutDiv("monerisCheckout");
                myCheckout.setCallback("page_loaded", testPageLoad);
                myCheckout.setCallback("payment_receipt", testPayReceipt);
                myCheckout.setCallback("payment_complete", testPayComplete);
                myCheckout.setCallback("cancel_transaction", testCancel);
                myCheckout.setCallback("error_event", testCancel);
                myCheckout.setCallback("payment_submitted", testPaymentSubmitted);
                myCheckout.startCheckout('<?php echo $token ?>');
                myCheckout.setCallback("page_closed", testCancel);

                function testPageLoad(res){

                                console.log(res)

                }

                function testPaySubmit(res){

                                console.log(res)

                }

                function testPayReceipt(res){

                                console.log(res)

                }

                function testPayComplete(res){

                                console.log(res)

                }

                function testPaymentSubmitted(res){

                                console.log(res)

                }

                function testCancel(res){

                                console.log(res)

                                //myCheckout.closeCheckout(JSON.parse(res).ticket)

                }

               
        
    </script>
</body>
</html>

75% is working properly.


The cart section is now an issue .

>-- 
>  //Aho
>


-- 
Member - Liberal International This is doctor@nk.ca Ici doctor@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 
If their only strength is in enforcing weakness, then they will preside over ruin. -unknown Beware https://mindspring.com

[toc] | [prev] | [next] | [standalone]


#19303

From"J.O. Aho" <user@example.net>
Date2023-02-06 18:14 +0100
Message-ID<k4cqqvFpoj5U1@mid.individual.net>
In reply to#19302
On 2/6/23 16:28, The Doctor wrote:
> In article <k4bnmkFffdeU1@mid.individual.net>,
> J.O. Aho <user@example.net> wrote:
>> On 06/02/2023 02:31, The Doctor wrote:
>>> And in the case of recurring items when being puleld from
>>> a form getting parsed?
>>
>> If you are thinking of post value which is an array, then the simplest
>> would be:
>> $a->cart = (object)$_POST['cart']; // this may give you object you don't
>> want
>>
>> Just keep in mind that the data is unsafe to use, you should do the long
>> way and validate the data and as you validate you can then add the
>> values to a temp variable and if all the cart items passes validation,
>> then add it to the main json object  (in this silly example the $a).
>>
>> If you get a new cart posted (updates), then  replace the current one
>> you have in the session, don't forget to validate the data before you
>> replace the cart section.  Only have the latest validated one
>> $a->cart = $validated_shopping_cart; // validated_shopping_cart is a
>> json object
>>
>> for duplicate entries you have to decide what to do, cast an error or
>> merge them together, that is up to you, but do not have duplicate items
>> in your json object.
>>
>> I hope that was the answer you was looking for.
>>
> 
> The cart is being derived from a previous form being passed into PHP!
> 
> 
> Just an update
> The cart section is now an issue .
> 

> 'cart' => array(
> (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);
> 
> 	$cart['items'][] =array(
>           'url' => "https://www.pdsolutions.ca/images/newwhiteheader.png",
>          'description' => $_POST['description'][$key],
>          'product_code' => $_POST['id'][$key],
>         'unit_cost' => $unit_cost,
>         'quantity' => $quantity,
> 	),
>      $newtotal += $subtotal;
>      $items_count++;
>      array_push(items,itemsubarray);
>    	}
>    }
>           
>          'subtotal' => $newtotal,
>      if(isset($_POST['with_gst'][$key])){
>          'tax'=>array(
>            'amount' => ($newtotal * 0.05),
>            'description' => "GST",
> 	  'rate' => "5.00",
>            ),
>      })
> ),

It's messy and a lot of hard coded things and I'm just guessing what you 
try to do, I think I would have done things differently, for example 
posting all the data maybe as a serialized json object from the client 
side, I would fetch the items value from the database together with 
description, I would have the tax rate in the database too, easy to 
change the the tax in the database when the the government changes it.

I made an small example that take the cart part, not sure if it's what 
you want to have, but at least should give you some idea and the reason 
I didn't use $_POST values is that it's easier to test things when you 
can modify values based on what you want and fine tune things till they 
are right, then you can modify the constructor to not take any 
arguments, I would make the "cart" class to hold all the values you need 
for your json.
If you need to use values between functions, then use private, for then 
they will not be included when you use the json_encode()

--- simple example ---
<?php

class cart {
   public $total = 0;
   public $items = [];
   private $uber = "man"; // this will not be included in the json output

   public function __construct($quantityArray, $gstArray, $descArray, 
$idArray) {
     foreach($quantityArray as $key => $num_items)
     {
         if($num_items == 0)
             continue;

         $has_tax = isset($gstArray[$key]);
         $unit_cost = $has_tax ? 63 : 60;
         $sub_total = $num_items * $unit_cost;
         $this->total = $sub_total;

         $item = [
             'url' => 
'https://www.pdsolutions.ca/images/newwhiteheader.png',
             'description' => $descArray[$key],
             'product_code' => $idArray[$key],
             'unit_cost' => number_format($unit_cost, 2),
             'quantity' => $num_items,
             'subtotal' => number_format($sub_total, 2),
         ];

         if($has_tax) {
             $item['tax'] = (object)[ 'amount' => 
number_format($sub_total * 0.05), 'description' => "GST", 'rate' => 
'5.00' ];
         }

         $this->items[] = (object)$item;

     }
   }
}

$q = [1, 2, 0];
$g = [1, 1, 0];
$d = ['banana', 'apple', 'car'];
$i = [22, 11, 33];

$cart = new cart($q, $g, $d, $i);
echo json_encode($cart, JSON_PRETTY_PRINT);
?>
--- eof ---

You can also see the use of number_format(), that just so we get the 
right number of decimals, I haven't bothered about rounding which would 
be sane. And it's the "items" that should be something similar to your cart.

-- 

  //Aho


[toc] | [prev] | [next] | [standalone]


#19305

Fromdoctor@doctor.nl2k.ab.ca (The Doctor)
Date2023-02-07 00:56 +0000
Message-ID<trs7ki$2pkv$28@gallifrey.nk.ca>
In reply to#19303
In article <k4cqqvFpoj5U1@mid.individual.net>,
J.O. Aho <user@example.net> wrote:
>On 2/6/23 16:28, The Doctor wrote:
>> In article <k4bnmkFffdeU1@mid.individual.net>,
>> J.O. Aho <user@example.net> wrote:
>>> On 06/02/2023 02:31, The Doctor wrote:
>>>> And in the case of recurring items when being puleld from
>>>> a form getting parsed?
>>>
>>> If you are thinking of post value which is an array, then the simplest
>>> would be:
>>> $a->cart = (object)$_POST['cart']; // this may give you object you don't
>>> want
>>>
>>> Just keep in mind that the data is unsafe to use, you should do the long
>>> way and validate the data and as you validate you can then add the
>>> values to a temp variable and if all the cart items passes validation,
>>> then add it to the main json object  (in this silly example the $a).
>>>
>>> If you get a new cart posted (updates), then  replace the current one
>>> you have in the session, don't forget to validate the data before you
>>> replace the cart section.  Only have the latest validated one
>>> $a->cart = $validated_shopping_cart; // validated_shopping_cart is a
>>> json object
>>>
>>> for duplicate entries you have to decide what to do, cast an error or
>>> merge them together, that is up to you, but do not have duplicate items
>>> in your json object.
>>>
>>> I hope that was the answer you was looking for.
>>>
>> 
>> The cart is being derived from a previous form being passed into PHP!
>> 
>> 
>> Just an update
>> The cart section is now an issue .
>> 
>
>> 'cart' => array(
>> (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);
>> 
>> 	$cart['items'][] =array(
>>           'url' => "https://www.pdsolutions.ca/images/newwhiteheader.png",
>>          'description' => $_POST['description'][$key],
>>          'product_code' => $_POST['id'][$key],
>>         'unit_cost' => $unit_cost,
>>         'quantity' => $quantity,
>> 	),
>>      $newtotal += $subtotal;
>>      $items_count++;
>>      array_push(items,itemsubarray);
>>    	}
>>    }
>>           
>>          'subtotal' => $newtotal,
>>      if(isset($_POST['with_gst'][$key])){
>>          'tax'=>array(
>>            'amount' => ($newtotal * 0.05),
>>            'description' => "GST",
>> 	  'rate' => "5.00",
>>            ),
>>      })
>> ),
>
>It's messy and a lot of hard coded things and I'm just guessing what you 
>try to do, I think I would have done things differently, for example 
>posting all the data maybe as a serialized json object from the client 
>side, I would fetch the items value from the database together with 
>description, I would have the tax rate in the database too, easy to 
>change the the tax in the database when the the government changes it.
>
>I made an small example that take the cart part, not sure if it's what 
>you want to have, but at least should give you some idea and the reason 
>I didn't use $_POST values is that it's easier to test things when you 
>can modify values based on what you want and fine tune things till they 
>are right, then you can modify the constructor to not take any 
>arguments, I would make the "cart" class to hold all the values you need 
>for your json.
>If you need to use values between functions, then use private, for then 
>they will not be included when you use the json_encode()
>
>--- simple example ---
><?php
>
>class cart {
>   public $total = 0;
>   public $items = [];
>   private $uber = "man"; // this will not be included in the json output
>
>   public function __construct($quantityArray, $gstArray, $descArray, 
>$idArray) {
>     foreach($quantityArray as $key => $num_items)
>     {
>         if($num_items == 0)
>             continue;
>
>         $has_tax = isset($gstArray[$key]);
>         $unit_cost = $has_tax ? 63 : 60;
>         $sub_total = $num_items * $unit_cost;
>         $this->total = $sub_total;
>
>         $item = [
>             'url' => 
>'https://www.pdsolutions.ca/images/newwhiteheader.png',
>             'description' => $descArray[$key],
>             'product_code' => $idArray[$key],
>             'unit_cost' => number_format($unit_cost, 2),
>             'quantity' => $num_items,
>             'subtotal' => number_format($sub_total, 2),
>         ];
>
>         if($has_tax) {
>             $item['tax'] = (object)[ 'amount' => 
>number_format($sub_total * 0.05), 'description' => "GST", 'rate' => 
>'5.00' ];
>         }
>
>         $this->items[] = (object)$item;
>
>     }
>   }
>}
>
>$q = [1, 2, 0];
>$g = [1, 1, 0];
>$d = ['banana', 'apple', 'car'];
>$i = [22, 11, 33];
>
>$cart = new cart($q, $g, $d, $i);
>echo json_encode($cart, JSON_PRETTY_PRINT);
>?>
>--- eof ---
>
>You can also see the use of number_format(), that just so we get the 
>right number of decimals, I haven't bothered about rounding which would 
>be sane. And it's the "items" that should be something similar to your cart.
>
>-- 
>
>  //Aho
>
>
>

Will try.  Basing this in the Moneris docs.
-- 
Member - Liberal International This is doctor@nk.ca Ici doctor@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 
Extremism is for those who love judging, and care nothing for accuracy. -unknown Beware https://mindspring.com

[toc] | [prev] | [next] | [standalone]


#19304

FromArno Welzel <usenet@arnowelzel.de>
Date2023-02-06 20:40 +0100
Message-ID<k4d3dvFtd5nU1@mid.individual.net>
In reply to#19302
The Doctor, 2023-02-06 16:28:

[...]
> The cart is being derived from a previous form being passed into PHP!

[...]
> $arr=array(
> 'store_id' => "store3",
> 'api_token' => "yesguy",
> 'checkout_id' => "chktGLFZQtore3",
> 'txn_total' => $_POST['charge_total'],
> 'environment' => "qa",
> 'action' => "preload",
> 'cart' => array( 
> (foreach ($_POST['quantity'] as $key => $value) {

Well - this will result in an array in the JSON named "cart".

> 	$cart['items'][] =array(

And this will also become an array named "items" inside "cart".

So if you don't want "cart" to be an array in the JSON, then don't
create it this way.

Learn how to read JSON and that the "punctuation" are not just
characters but have a specific meaning. In this case you created an
array and the JSON will have an array as well - indicated by "[" and "]".

-- 
Arno Welzel
https://arnowelzel.de

[toc] | [prev] | [next] | [standalone]


#19306

Fromdoctor@doctor.nl2k.ab.ca (The Doctor)
Date2023-02-07 00:57 +0000
Message-ID<trs7m0$2pkv$29@gallifrey.nk.ca>
In reply to#19304
In article <k4d3dvFtd5nU1@mid.individual.net>,
Arno Welzel  <usenet@arnowelzel.de> wrote:
>The Doctor, 2023-02-06 16:28:
>
>[...]
>> The cart is being derived from a previous form being passed into PHP!
>
>[...]
>> $arr=array(
>> 'store_id' => "store3",
>> 'api_token' => "yesguy",
>> 'checkout_id' => "chktGLFZQtore3",
>> 'txn_total' => $_POST['charge_total'],
>> 'environment' => "qa",
>> 'action' => "preload",
>> 'cart' => array( 
>> (foreach ($_POST['quantity'] as $key => $value) {
>
>Well - this will result in an array in the JSON named "cart".
>
>> 	$cart['items'][] =array(
>
>And this will also become an array named "items" inside "cart".
>
>So if you don't want "cart" to be an array in the JSON, then don't
>create it this way.
>
>Learn how to read JSON and that the "punctuation" are not just
>characters but have a specific meaning. In this case you created an
>array and the JSON will have an array as well - indicated by "[" and "]".
>

Again basing this on Moneris prereqs.

>-- 
>Arno Welzel
>https://arnowelzel.de
>


-- 
Member - Liberal International This is doctor@nk.ca Ici doctor@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 
Extremism is for those who love judging, and care nothing for accuracy. -unknown Beware https://mindspring.com

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.php


csiph-web