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

  • SEARCH
  • Home
  • 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 8311599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:52:06+00:00 2026-06-08T19:52:06+00:00

I have invalid external json data, without double quotes around names. Example: { data:

  • 0

I have invalid external json data, without double quotes around names.

Example:

{
  data: [
    {
      idx: 0,
      id: "0",
      url: "http://247wallst.com/",
      a: [
        {
          t: "Title",
          u: "http://247wallst.com/2012/07/30/",
          sp: "About"
        }
      ],
      doc_id: "9386093612452939480"
    },
    {
      idx: 1,
      id: "-1"
    }
  ],
  results_per_page: 10,
  total_number_of_news: 76,
  news_per_month: [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
  result_start_num: 2,
  result_end_num: 2,
  result_total_articles: 76
}

As you see a lot of names like data,idx,id,url and others are not double quoted, so this makes this json invalid.
How can I make this external json valid? I already tried str_replace, replacing ‘{‘ to ‘{“‘ and ‘:’ to ‘”:’ adding double quotes around unquoted names, but this messes up some already double quoted variables.

How can I make this json valid so I can read this data with PHP json_decode? I’m not very familiar with preg_replace..

Valid json will look like:

{
  "data": [
    {
      "idx": 0,
      "id": "0",
      "url": "http://247wallst.com/",
      "a": [
        {
          "t": "Title",
          "u": "http://247wallst.com/2012/07/30/",
          "sp": "About"
        }
      ],
      "doc_id": "9386093612452939480"
    },
    {
      "idx": 1,
      "id": "-1"
    }
  ],
  "results_per_page": 10,
  "total_number_of_news": 76,
  "news_per_month": [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
  "result_start_num": 2,
  "result_end_num": 2,
  "result_total_articles": 76
}

Please suggest me some php preg_replace function.

Data source:
http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1

  • 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-06-08T19:52:08+00:00Added an answer on June 8, 2026 at 7:52 pm

    With preg_replace you can do:

    json_decode(preg_replace('#(?<pre>\{|\[|,)\s*(?<key>(?:\w|_)+)\s*:#im', '$1"$2":', $in));
    

    Since the above example won’t work with real data (the battle plans seldom survive first contact with the enemy) heres my second take:

    $infile = 'http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1';
    
    // first, get rid of the \x26 and other encoded bytes.
    $in = preg_replace_callback('/\\\x([0-9A-F]{2})/i',
        function($match){
            return chr(intval($match[1], 16));
        }, file_get_contents($infile));
    
    $out = $in;
    
    // find key candidates
    preg_match_all('#(?<=\{|\[|,)\s*(?<key>(?:\w|_)+?)\s*:#im', $in, $m, PREG_OFFSET_CAPTURE);
    
    $replaces_so_far = 0;
    // check each candidate if its in a quoted string or not
    foreach ($m['key'] as $match) {
        $position = $match[1] + ($replaces_so_far * 2); // every time you expand one key, offsets need to be shifted with 2 (for the two " chars)
        $key = $match[0];
        $quotes_before = preg_match_all('/(?<!\\\)"/', substr($out, 0, $position), $m2);
        if ($quotes_before % 2) { // not even number of not-escaped quotes, we are in quotes, ignore candidate
            continue;
        }
        $out = substr_replace($out, '"'.$key.'"', $position, strlen($key));
        ++$replaces_so_far;
    }
    
    var_export(json_decode($out, true));
    

    But since google offers this data in RSS feed, i would recommend you to use that one if it works for your usecase, this is just for fun (-:

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

Sidebar

Related Questions

Here's an example: http://jsfiddle.net/7g8GD/1/ Once you have entered an invalid input and hit enter
I'm currently puling data from an external XML file and converting it to JSON
How to deal with invalid UTF-8 sequences in data from external file / external
I have a invalid XML like this Warning: count() [function.count]: Node no longer exists
I am wondering about the performance of this index: I have an Invalid varchar(1)
I want to throw an exception to say that we have an invalid email
I have the following invalid XML file: <?xml version=1.0 encoding=utf-8 ?> <Page num=1 crop_box=0,
Several of my overriden Equals methods have started throwing Invalid Cast Exceptions - Unable
I have been getting an invalid character! on what looks like the £ sign
I have an application that creates invalid Directories... e.g. c:\Program Files\somedirectory. - the period

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.