0

I have this JSON file

{
 1 : {
 name: "John Doe",
 birthYear: "1990",
 reqion: "USA"
 phone: "604789577", 
 },
 2 : {
 name: "Jose Dirack",
 birthYear: "1970",
 reqion: "Europe"
 phone: "768789577", 
 }
}

And json_decode() is uanble to decode it. Do you see why? Have you any idea how to fix it?

Mat
208k41 gold badges409 silver badges423 bronze badges
asked Feb 8, 2012 at 10:34
3
  • 2
    reqion: "USA" phone: "604789577", is missing a comma Commented Feb 8, 2012 at 10:36
  • 2
    JSONLint (jsonlint.com) is a useful tool for such situations. Commented Feb 8, 2012 at 10:37
  • "reqion" should probably be "region", although of course that's not what's wrong with the JSON. It's just wrong. :-) Commented Feb 8, 2012 at 10:40

3 Answers 3

3

The keys in objects need to be properly encoded strings:

{
 "1" : {
 "name": "John Doe",
 "birthYear": "1990",
 "reqion": "USA",
 "phone": "604789577"
 },
 "2" : {
 "name": "Jose Dirack",
 "birthYear": "1970",
 "reqion": "Europe",
 "phone": "768789577"
 }
}

There was also a typo with the separating commas.

answered Feb 8, 2012 at 10:37
Sign up to request clarification or add additional context in comments.

4 Comments

Careful now, there were still some small problems (see my edit)
+1 for showing the corrected version and linking to the little-known RFC.
@alex You would just had scrolled down the page you have linked to. There is also a link to the RFC 4627 application/json (although just the plain text format).
@Gumbo I shall pay more attention to site's footers from here on in.
1

The keys must be quoted with double quotes as per the JSON spec. If the outer object is meant to be an array, swap the {} with [] and drop the explicit numbering.

You are also missing a comma after reqion.

You also have trailing commas which shouldn't be there.

answered Feb 8, 2012 at 10:36

1 Comment

Yeah, Crockford really blew it by not allowing trailing commas.
0

Try this:

[
 {
 "name": "John Doe",
 "birthYear": "1990",
 "reqion": "USA",
 "phone": "604789577" 
 },
 {
 "name": "Jose Dirack",
 "birthYear": "1970",
 "reqion": "Europe",
 "phone": "768789577" 
 }
]
answered Feb 8, 2012 at 10:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.