79792463

Date: 2025-10-16 18:36:05
Score: 0.5
Natty:
Report link

This converts All Keys that are type Numeric into type Strings.
Uses @sln JSON Validation and Error detection Regex Functions.
These functions parse JSON to the specification and can be used as a Query/Replace
engine on steroids.

For the details on how this regex works, and more examples see :
https://stackoverflow.com/a/79785886/15577665

Regex Demo : https://regex101.com/r/klszdB/1
Php Demo : https://onlinephp.io/c/62561

(?:(?=(?&V_Obj)){|(?!^)\G(?&Sep_Obj)\s*)(?:(?&V_KeyVal)(?&Sep_Obj))*?\s*(?&Str)\s*:\s*\K(?&Numb)(?(DEFINE)(?<Sep_Ary>\s*(?:,(?!\s*[}\]])|(?=\])))(?<Sep_Obj>\s*(?:,(?!\s*[}\]])|(?=})))(?<Er_Obj>(?>{(?:\s*(?&Str)(?:\s*:(?:\s*(?:(?&Er_Value)|(?<Er_Ary>\[(?:\s*(?:(?&Er_Value)|(?&Er_Ary)|(?&Er_Obj))(?:(?&Sep_Ary)|(*ACCEPT)))*(?:\s*\]|(*ACCEPT)))|(?&Er_Obj))(?:(?&Sep_Obj)|(*ACCEPT))|(*ACCEPT))|(*ACCEPT)))*(?:\s*}|(*ACCEPT))))(?<Er_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)))(?<Str>(?>"[^\\"]*(?:\\[\s\S][^\\"]*)*"))(?<Numb>(?>[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|(?:[eE][+-]?\d+)))(?<V_KeyVal>(?>\s*(?&Str)\s*:\s*(?&V_Value)\s*))(?<V_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)|(?&V_Obj)|(?&V_Ary)))(?<V_Ary>\[(?>\s*(?&V_Value)(?&Sep_Ary))*\s*\])(?<V_Obj>{(?>(?&V_KeyVal)(?&Sep_Obj))*\s*}))

Replace "$0"

Regex Comments

(?:                                   # ----------
   (?= (?&V_Obj) )                      # Should be a valid object ahead
   {                                    # Open Object
 |                                 # or,
   (?! ^ )                              # Here we are in a valid JSON Object
   \G                                   # Continuation anchor, where we last left off
   (?&Sep_Obj) \s*                      # Object Separator after last match
)                                     # ----------

(?: (?&V_KeyVal) (?&Sep_Obj) )*?      # Drill down to the next key that is a Number Type
\s* (?&Str) \s* : \s* 

\K                                    # Stop recording here, match just the Number
(?&Numb) 


# JSON functions -
# ------------------ 
(?(DEFINE)(?<Sep_Ary>\s*(?:,(?!\s*[}\]])|(?=\])))(?<Sep_Obj>\s*(?:,(?!\s*[}\]])|(?=})))(?<Er_Obj>(?>{(?:\s*(?&Str)(?:\s*:(?:\s*(?:(?&Er_Value)|(?<Er_Ary>\[(?:\s*(?:(?&Er_Value)|(?&Er_Ary)|(?&Er_Obj))(?:(?&Sep_Ary)|(*ACCEPT)))*(?:\s*\]|(*ACCEPT)))|(?&Er_Obj))(?:(?&Sep_Obj)|(*ACCEPT))|(*ACCEPT))|(*ACCEPT)))*(?:\s*}|(*ACCEPT))))(?<Er_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)))(?<Str>(?>"[^\\"]*(?:\\[\s\S][^\\"]*)*"))(?<Numb>(?>[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|(?:[eE][+-]?\d+)))(?<V_KeyVal>(?>\s*(?&Str)\s*:\s*(?&V_Value)\s*))(?<V_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)|(?&V_Obj)|(?&V_Ary)))(?<V_Ary>\[(?>\s*(?&V_Value)(?&Sep_Ary))*\s*\])(?<V_Obj>{(?>(?&V_KeyVal)(?&Sep_Obj))*\s*}))

Php source :

<?php
// Enter your code here, enjoy!
$Rx = '/(?:(?=(?&V_Obj)){|(?!^)\G(?&Sep_Obj)\s*)(?:(?&V_KeyVal)(?&Sep_Obj))*?\s*(?&Str)\s*:\s*\K(?&Numb)(?(DEFINE)(?<Sep_Ary>\s*(?:,(?!\s*[}\]])|(?=\])))(?<Sep_Obj>\s*(?:,(?!\s*[}\]])|(?=})))(?<Er_Obj>(?>{(?:\s*(?&Str)(?:\s*:(?:\s*(?:(?&Er_Value)|(?<Er_Ary>\[(?:\s*(?:(?&Er_Value)|(?&Er_Ary)|(?&Er_Obj))(?:(?&Sep_Ary)|(*ACCEPT)))*(?:\s*\]|(*ACCEPT)))|(?&Er_Obj))(?:(?&Sep_Obj)|(*ACCEPT))|(*ACCEPT))|(*ACCEPT)))*(?:\s*}|(*ACCEPT))))(?<Er_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)))(?<Str>(?>"[^\\"]*(?:\\[\s\S][^\\"]*)*"))(?<Numb>(?>[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|(?:[eE][+-]?\d+)))(?<V_KeyVal>(?>\s*(?&Str)\s*:\s*(?&V_Value)\s*))(?<V_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)|(?&V_Obj)|(?&V_Ary)))(?<V_Ary>\[(?>\s*(?&V_Value)(?&Sep_Ary))*\s*\])(?<V_Obj>{(?>(?&V_KeyVal)(?&Sep_Obj))*\s*}))/';

$json = '
{
   "first_name":"sample",
   "last_name": "lastname",
   "integer" : 100,
   "float" : 1555.20,
   "createddate":"2015-06-25 09:57:28",
   "asder" : 
     [ 
        200, 
        100,
        {
          "nother1" : "here1",
          "digi1" : 900e10,
          "nother2" : "here2",
          "digi2" : 3.14
        },
        400,
        37
     ]
}';

$new_json = preg_replace( $Rx, '"$0"', $json );

var_dump( $new_json );

Output :

string(384) "
{
   "first_name":"sample",
   "last_name": "lastname",
   "integer" : "100",
   "float" : "1555.20",
   "createddate":"2015-06-25 09:57:28",
   "asder" : 
     [ 
        200, 
        100,
        {
          "nother1" : "here1",
          "digi1" : "900e10",
          "nother2" : "here2",
          "digi2" : "3.14"
        },
        400,
        37
     ]
}"
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @sln
  • High reputation (-1):
Posted by: sln