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


Groups > comp.lang.php > #19288

Re: Stripping a JSON of extra characters in PHP

From Arno Welzel <usenet@arnowelzel.de>
Newsgroups comp.lang.php
Subject Re: Stripping a JSON of extra characters in PHP
Date 2023-02-03 18:12 +0100
Message-ID <k44tjfFku3dU3@mid.individual.net> (permalink)
References <tre1ei$2lti$75@gallifrey.nk.ca> <875yclmkpv.fsf@bsb.me.uk>

Show all headers | View raw


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

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