last day (15 days later) » 

9:21 AM
-2
Q: Json decoding failing - need pointer

RichieI have spent all day long on this, researching every single json decoding I could find on this website. I need help, please. here is a sample they gave me: { "txs": [{ "txid": "33d5d9af65fe63b12e1a4d67f6b05fcf01428764db840463aba621daa65323d3", "status": "good", "t3a_id": "164f...

 
I know, so you gave me a negative, but did not show me the correct way?
I did not close it, I'm looking at the answers.
I looked at that page and tried everything and none of them worked for me.
Thank you Leo :) I'll try it and let you know :)
 
@leo OPs don't close questions, people with the relevant privileges close them when they're not answerable, are off-topic, or they're a duplicate of an existing one. This one is a duplicate - OP can study the principles there to understand the basics and, having understood them, can then work out how to decode their own data - or any other similar data - quite easily. You can get more info in the help centre if you're unsure how the closure and re-opening system works.
 
Okay Leo, I did this: $_txid = $obj->txs[0]->txid; $_item_name = $obj->cart[0]->items->label; $_item_number = $obj->cart[0]->items->sku; and I got this error now: Fatal error: Uncaught Error: Cannot use object of type stdClass as array in line 178, 178 is this one: $_item_name = $obj->cart[0]->items->label; so how do I do those ones? [0] on each?
 
@leo I didn't provide the link. But it will help, because what it actually explains is how to decode said JSON, and then access the relevant parts of the resulting php array/object (by reading and understanding reading either the original JSON or the dumped php var to see the structure and then apply some code to get data out of that type of structure. The main answer is a really thorough but straightforward guide, if you read it.
 
I did a dump and it is just a bunch of garbage, I don't understand it. object, string, bool, lots of data, fills the page. I cannot make heads or tails out of it. is there a way to format it?
 
9:21 AM
Normally it is formatted neatly if you view it in a web page. But it's not garbage, it's the content of each item in your php variable, including property names, data types, and the actual values.
 
Adyson, wow, you are the man, I Did read all of it and I still cannot figure it out. I did the data_dump and it is a mess. Anyway to format it to easily see what is what? See the mess: screencast.com/t/aXqKC2Org8
 
stackoverflow.com/questions/17902608/… might also help you (and you can google other articles about how to interpret var_dump output). Or just understand your JSON instead and infer the php structure from that (as per the guidance in the duplicate)
 
ADyson, is there a way to just get what I need from json without having to do a json_decode? I tried just building a function that went through it all by exploding it, and going through everything between : but that wasted me 3 hours. :(
IMSoP that is probably a great idea, I taught myself everything I know by starting with hello world... but sleep apnea ruined my brain, I have such a hard time focusing it is crazy.
 
The main thing you seem not to have noticed in your JSON is that "txs" and "items" are both arrays, so they don't have named properties. Do you understand what [ and ] denote in JSON?
, is there a way to just get what I need from json without having to do a json_decode....not unless you want to spend all year writing your own JSON parser instead. Not much point reinventing that wheel though when json_decode works perfectly well
 
I believe [ opens a new data set, right? and the ] closes it?
 
9:21 AM
Well the correct term is "array". i.e. a list. In your examples it happens there is only one item within each list, but it's possible there could be many items. Therefore asking for {"txs"}{"txid"} doesn't work because the list doesn't have an txid property directly, and it doesn't know which item in the list (from the potentially multiple items) you want to get that property from. You need to either a) loop through all the items if you need the value from each one of them, or b) if you know you just want the first item, refer to to that entry by its number (e.g. 0) before the property.
3v4l.org/67Fsv is what you need I think. First I got rid of the obsolete, deprecated (and overly verbose) bracket-and-quote syntax for accessing object properties, to make the code clearer. Second, I added in the [0] in the right places to get the first items from each of those lists (txs and items) and then read the named property from that first item. Hopefully now if you go back to the duplicate question and read the section of the answer about arrays it will make more sense how you have an example.
 
Okay, so then would I do it like this:

$txid = $obj['txs'][2]; // if $txid was the 3rd item in the list?
 
Yes
 
Excellent, I'll try it now and see if that removes the error I keep getting. Thank you. brb
 
Let me know. Meanwhile I strongly suggest you to use the visualiser suggested in the duplicate - array.include-once.org . Paste your JSON in there and it gives you the equivalent PHP data dump, and if you hover your mouse over each item it suggests sample code to access it.
 
Where the cart is:
"cart": {
then "items": [
then{
"sku": "1",
"label": "30,000 Banner Ad Impressions",

would it be: $obj['cart'][0][1]
because [0] is the first one, then [1] is the second one under items?
cool, I'll check that out :) thank you
awesome resource
 
9:30 AM
cart isn't an array (there's no [ ] on it) so no that's not right
Look a bit more carefully at the data!
 
I used that page you showed me and that worked!!!!! TY, TY, TY!!! I'll sleep good now!!!\
TY MORE THAN YOU KNOW
$obj->cart->items[0]->label


$obj->txs[0]->txid
those worked
 
9:53 AM
Indeed. Same as in the demo I gave you :-). Do you now also understand WHY it worked? So that you could understand some different JSON and solve that too?
 
leo
10:34 AM
Richie, it worked but I think it's not right in terms of the code you are writing - you will use only the first array elements and skip others.
 
 
7 hours later…
5:44 PM
I am so appreciative of you both. Yes I understand it now, I see how it works. so I probably won't have as many issues now. :) Thank you again. Lifesavers
 

last day (15 days later) »