Search code examples
phpjsonstring-parsing

Parsing (non-standard) json to array/object


I have string like this:

['key1':'value1', 2:'value2', 3:$var, 4:'with\' quotes', 5:'with, comma']

And I want to convert it to an array like this:

$parsed = [
    'key1' => 'value1',
    2      => 'value2',
    3      => '$var',
    4      => 'with\' quotes',
    5      => 'with, comma',
];

How can I parse that? Any tips or codes will be appreciated.

What can't be done?

  • Using standard json parsers
  • eval()
  • explode() by , and explode() by :

Solution

  • As you cannot use any pre-built function, like json_decode, you'll have to try and find the most possible scenarios of quoting, and replace them with known substrings.

    Given that all of the values and/or keys in the input array are encapsulated in single quotes:

    Please note: this code is untested

    <?php
        $input = "[ 'key1':'value1', 2:'value2', 3:$var, 4:'with\' quotes', 5: '$var', 'another_key': 'something not usual, like \'this\'' ]";
    
        function extractKeysAndValuesFromNonStandardKeyValueString ( $string ) {
    
            $input = str_replace ( Array ( "\\\'", "\'" ), Array ( "[DOUBLE_QUOTE]", "[QUOTE]" ), $string );
            $input_clone = $input;
    
            $return_array = Array ();
    
            if ( preg_match_all ( '/\'?([^\':]+)\'?\s*\:\s*\'([^\']+)\'\s*,?\s*/', $input, $matches ) ) {
    
                foreach ( $matches[0] as $i => $full_match ) {
    
                    $key = $matches[1][$i];
                    $value = $matches[2][$i];
    
                    if ( isset ( ${$value} ) $value = ${$value};
                    else $value = str_replace ( Array ( "[DOUBLE_QUOTE]", "[QUOTE]" ), Array ( "\\\'", "\'" ), $value );
    
                    $return_array[$key] = $value;
    
                    $input_clone = str_replace ( $full_match, '', $input_clone );
                }
    
                // process the rest of the string, if anything important is left inside of it
                if ( preg_match_all ( '/\'?([^\':]+)\'?\s*\:\s*([^,]+)\s*,?\s*/', $input_clone, $matches ) ) {
                    foreach ( $matches[0] as $i => $full_match ) {
    
                        $key = $matches[1][$i];
                        $value = $matches[2][$i];
    
                        if ( isset ( ${$value} ) $value = ${$value};
    
                        $return_array[$key] = $value;
                    }
                }
            }
    
    
            return $return_array;
    
        }
    

    The idea behind this function is to first replace all the possible combinations of quotes in the non-standard string with something you can easily replace, then perform a standard regexp against your input, then rebuild everything assuring you're resetting the previously replaced substrings