Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1111027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:29:02+00:00 2026-05-17T02:29:02+00:00

I am looking for a Regex that allows me to validate json. I am

  • 0

I am looking for a Regex that allows me to validate json.

I am very new to Regex’s and i know enough that parsing with Regex is bad but can it be used to validate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:29:02+00:00Added an answer on May 17, 2026 at 2:29 am

    Yes, a complete regex validation is possible.

    Some modern regex implementations allow for recursive regular expressions, which can verify a complete JSON serialized structure. The json.org specification makes it quite straightforward.

    $pcre_regex = '/
        (?(DEFINE)
            (?<ws>      [\t\n\r ]* )
            (?<number>  -? (?: 0|[1-9]\d*) (?: \.\d+)? (?: [Ee] [+-]? \d++)? )    
            (?<boolean> true | false | null )
            (?<string>  " (?: [^\\\\"\x00-\x1f] | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9A-Fa-f]{4} )* " )
            (?<pair>    (?&ws) (?&string) (?&ws) : (?&value) )
            (?<array>   \[ (?: (?&value) (?: , (?&value) )* )? (?&ws) \] )
            (?<object>  \{ (?: (?&pair) (?: , (?&pair) )* )? (?&ws) \} )
            (?<value>   (?&ws) (?: (?&number) | (?&boolean) | (?&string) | (?&array) | (?&object) ) (?&ws) )
        )
        \A (?&value) \Z
        /sx';
    

    The example above uses the Perl 5.10/PCRE2 subroutine call syntax to simplify the expression and improve readability. It works quite well in PHP with the PCRE functions. Should work almost unmodified in Perl (provided one replaces 4-backslash sequences '\\\\' with 2-backslash sequences '\\' in the <string> subroutine); and can be adapted for other languages (e.g. Ruby, or those for which PCRE bindings are available).

    This regex passes all tests from the JSON.org test suite (see link at the end of the page) as well as those from Nicolas Seriot‘s JSON Parser test suite.1

    Simpler RFC4627 verification

    A simpler approach is the minimal consistency check as specified in RFC4627, section 6. It’s however just intended as security test and basic non-validity precaution:

    var jsonCode = /* untrusted input */;
    
    var jsonObject = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
        jsonCode.replace(/"(\\.|[^"\\])*"/g, '')))
        && eval('(' + jsonCode + ')');
    

    1 With the exception of two cases whose input is very large, causing the regex to time out. More generally, this approach is bound to fail on inputs large enough to hit the resource limits of the matching engine (either in time or space).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a regex that will allow me to validate whether or not
I am looking for a regex that will match a string that starts with
I am looking for a regex that will find repeating letters. So any letter
I am looking for a regex pattern that would match several different combinations of
I am looking for a regex statement that will let me extract the HTML
I'm looking for a multiline regex that will match occurrences after a blank line.
Looking for a bit of regex help. I'd like to design an expression that
I have a regex that I am using to validate email addresses. I like
I'm looking for the regex to validate hostnames. It must completely conform to the
I'm looking for a regex to validate initials. The only format I want it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.