0
{"": "attachment-2","": "attachment-1"}

I am getting this JSON-encoded string (or an oher format... let me know) from parsing a mail and I can not change it. How can I decode it?

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
TechCare99
  • 1,763
  • 6
  • 24
  • 33
  • `json_decode` in PHP see : http://php.net/manual/en/function.json-decode.php – Baba Oct 11 '12 at 10:57
  • I'm not sure what you're trying to do... What is your problem? – Aleks G Oct 11 '12 at 10:57
  • As far as I can see this JSON is invalid anyway. – KingCrunch Oct 11 '12 at 10:58
  • Note though that this is not a properly formatted json string. Proper JSON cannot have duplicate keys. – Aleks G Oct 11 '12 at 10:58
  • 4
    Did you guys even *try* the code you suggest @mogria and baba? You only get one element since duplicate keys are not supported. - and according to jsonlint it's not syntactically invalid. it just silently overwrites the first element with the same key – ThiefMaster Oct 11 '12 at 10:59
  • Screen shot of what I am getting after parsing the mail ! i want to decode content-id-map http://easycaptures.com/fs/uploaded/685/5568730832.png – TechCare99 Oct 11 '12 at 11:04

3 Answers3

5

You cannot use a JSON parser for this as it will always overwrite the first element due to the same keys. The only proper solution would be asking whoever creates that "JSON" to fix his code to either use an array or an object with unique keys.

If that's not an option the only thing you can do it rewriting the JSON to have unique keys before parsing it using json_decode()

Assuming it always gives you proper JSON and the duplicate keys are always empty you can replace "": with "random-string": - preg_replace_callback() is your friend in this case:

$lame = '{"": "attachment-2","": "attachment-1"}';
$json = preg_replace_callback('/"":/', function($m) {
    return '"' . uniqid() . '":';
}, $lame);
var_dump(json_decode($json));

Output:

object(stdClass)#1 (2) {
  ["5076bdf9c2567"]=>
  string(12) "attachment-2"
  ["5076bdf9c25b5"]=>
  string(12) "attachment-1"
}
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • Screen shot of what I am getting after parsing the mail ! i want to decode content-id-map easycaptures.com/fs/uploaded/685/5568730832.png – TechCare99 Oct 11 '12 at 11:11
  • this looks right way, i can go . Can u help me to built a function to so that after result ...I get the same key as its value. – TechCare99 Oct 11 '12 at 11:24
1

This JSON response is invalid as @ThiefMaster mentioned, because JSON doesn't support duplicated keys.

You should contact the service you're trying to request this response from.

In case you have a valid JSON response you can decode it using the json_decode function which returns an object or an array (depends on the second parameter);

For example: (Object)

$json_string = '{"keyOne": "attachment-2","keyTwo": "attachment-1"}';
$decoded = json_decode($json_string);

print $obj->keyOne; //attachment-2
print $obj->keyTwo; //attachment-1

Another option is to write your own decoder function.

Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39
0

Decode it yourself?

$myStr = '{"": "attachment-2","": "attachment-1"}';
$vars = explode(',',$myStr);
$arr = array();
foreach($vars as $v){
    list($key,$value) = explode(':',$v);
    $key = substr($key,strpos($key,'"'),strpos($key,'"')-strrpos($key,'"'));
    $value = substr($value,strpos($value,'"'),strpos($value,'"')-strrpos($value,'"'));
    if($key=='')$arr[] = $value;
    else $arr[$key] = $value;
}
Salketer
  • 14,263
  • 2
  • 30
  • 58