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


Groups > comp.lang.php > #19300

Re: Stripping a JSON of extra characters in PHP

From doctor@doctor.nl2k.ab.ca (The Doctor)
Newsgroups comp.lang.php
Subject Re: Stripping a JSON of extra characters in PHP
Date 2023-02-06 01:31 +0000
Organization NetKnow News
Message-ID <trpl8k$12si$72@gallifrey.nk.ca> (permalink)
References <tre1ei$2lti$75@gallifrey.nk.ca> <k49veoF1ne8U3@mid.individual.net>

Show all headers | View raw


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

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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

csiph-web