0

I have this JSON string:

{
    "product": [
        {
            "id": "1",
            "title": "producta",
            "size": "50",
            "weight": "1000",
            "price": "30",
            "quantity": "100",
            "cartID": "1"
        },
        {
            "id": "1",
            "title": "producta",
            "size": "50",
            "weight": "1000",
            "price": "30",
            "quantity": "100",
            "cartID": "2"
        }
    ]
}

When I use the PHP function json_decode($products, true), and then I re-encode it using json_encode($products), the string becomes this:

{
    "product": {
        "1": {
            "id": "2",
            "title": "producta",
            "size": "50",
            "weight": "1000",
            "price": "30",
            "quantity": "100",
            "cartID": "2"
        },
        "2": {
            "id": "1",
            "title": "producta",
            "size": "50",
            "weight": "1000",
            "price": "30",
            "quantity": "100",
            "cartID": "3"
        }
    }
}

After decoding and re-encoding, it adds a key to every "product"

Is there a way around this?

TaylorMac
  • 8,882
  • 21
  • 76
  • 104
  • Holy bleepity bleep Batman! That's horrible! – Ignacio Vazquez-Abrams Feb 17 '12 at 04:44
  • :/ Is there a mistake in the way that it is originally formatted? – TaylorMac Feb 17 '12 at 04:45
  • @TaylorMac: No, it's just a new way for me to hate PHP. – Ignacio Vazquez-Abrams Feb 17 '12 at 04:47
  • @RPM: "Usually you retrieve JSON data with JavaScript" Really? I usually use Python to pull it from web services. – Ignacio Vazquez-Abrams Feb 17 '12 at 04:48
  • Well the shopping cart on my website stores the cart items in JSON format (as shown above). The purpose of the encode and decode functions in PHP is to handle data like this received from client side processing. – TaylorMac Feb 17 '12 at 04:49
  • 2
    All PHP arrays **MUST** have keys. json_encode() will do an exact reproduction of whatever's stored internally, and that includes those keys. – Marc B Feb 17 '12 at 04:55
  • @Marc: But then why wouldn't it just decode with an initial key of 0 in the first place, meaning that it wouldn't need to convert it from an array into an object? HATE HATE HATE... – Ignacio Vazquez-Abrams Feb 17 '12 at 04:58
  • Well then that is fine... but how do I convert it back? – TaylorMac Feb 17 '12 at 05:00
  • Sorry for just asking for an answer I have just been frustrated with this for hours – TaylorMac Feb 17 '12 at 05:00
  • @Ignacio: I can't reproduce the OP's stuff even after cutting/pasting the sample string and doing `echo json_encode(json_decode($j, true))`. I don't get the extra key stuff, on php 5.3.6 – Marc B Feb 17 '12 at 05:02
  • I face the same issue, when I swipe array_keys with my array_search - key values and getting 0, 1 etc key values with my resulting json_encoded array. Any suggestions pls? My question link: http://stackoverflow.com/questions/24228468/php-array-search-appends-keys-with-json-encode-function-parsing-issue – Aditya P Bhatt Jun 15 '14 at 10:03

2 Answers2

2

Posting this here since it'd be too ugly in a comment, but this is what I get on PHP 5.3.6 after doing a echo json_encode(json_decode('...your json...', true));:

{"product":  [
     {"id":"1","title":"producta","size":"50","weight":"1000","price":"30","quantity":"100","cartID":"1"},
     {"id":"1","title":"producta","size":"50","weight":"1000","price":"30","quantity":"100","cartID":"2"}
]}

Note the lack of extra keys. Are you doing any manipulations on the decoed array before re-encoding?

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • I'm glad it is working for you. I am not making any manipulations... it may be the PHP version :/ – TaylorMac Feb 17 '12 at 05:11
  • I face the same issue, when I swipe array_keys with my array_search - key values and getting 0, 1 etc key values with my resulting json_encoded array. Any suggestions pls? My question link: http://stackoverflow.com/questions/24228468/php-array-search-appends-keys-with-json-encode-function-parsing-issue – Aditya P Bhatt Jun 15 '14 at 10:03
0

You are probably using mysqli_fetch_array instead of mysqli_fetch_assoc. Meaning u are json decoding a key arreay instead of an associative array