Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐞 it seems that there is a bug ? #513

Closed
xgqfrms-GitHub opened this issue Apr 10, 2017 · 5 comments
Closed

🐞 it seems that there is a bug ? #513

xgqfrms-GitHub opened this issue Apr 10, 2017 · 5 comments

Comments

@xgqfrms-GitHub
Copy link

xgqfrms-GitHub commented Apr 10, 2017

🐞

why this codes isn't a validate format ?

"profile": { 
    "name": "typicode1",
    "name": "typicode2",
    "name": "typicode3"
}

only this format is OK (object in array)

"profile": [
    { 
        "name": "typicode1"
    },
    { 
        "name": "typicode2"
    },
    { 
        "name": "typicode3"
    }
]
@arthur-melo
Copy link

In the first sample, you are declaring a single object profile with 3 key-value pairs name. Although it is a valid JSON format, a parser may complain about colliding keys (Which I guess the json-server parser does, and that's why you're asking).

The second sample works because you are declaring an array of objects, and they have their own "space" (Delimited by the { ... }). This means that you can have repeated identifier keys, as long as they are in their own namespace, and they don't collide within themselves.

For instance:

{
  "someObject": {
    "property": "A",
    "property": "B"  // <--- Collision
  }
}

There's a collision on the property key.

Using an array:

{
  "someArray": [
    { "name": "A", "value": "1" },
    { "name": "A", "value": "2" }, // <-- Repeated names property, but valid!
    { "name": "B", "value": "3", "value": "4" } // <-- Collision
  ]
}

There's a collision in the property value on the third objectB.

If you want to validate your code, I recommend using some online linter like this one.

More info on the subject: http://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object

@xgqfrms-GitHub
Copy link
Author

😄 Thanks for your time! 👍

This is a very good explanation!

However, I still have some confused, could you give more explain?

db.json

{
    "profile": {
        "name1": "typicode1",
        "name2": "typicode2",
        "name3": "typicode3"
    },
    "employess": [
        {
            "id": 0,
            "firstName": "Jaiden",
            "lastName": "Williamson",
            "email": "Kiera.Prosacco70@hotmail.com"
        },
        {
            "id": 1,
            "firstName": "Elody",
            "lastName": "Leuschke",
            "email": "Ena_Beier22@gmail.com"
        }
    ]
}

http://localhost:3000/profile?q=typicode2

{
    "name1": "typicode1",
    "name2": "typicode2",
    "name3": "typicode3"
}

How can I get this result ?

http://localhost:3000/profile?q=typicode2

{
    "name2": "typicode2"
}

@arthur-melo
Copy link

arthur-melo commented Apr 11, 2017

You can use 3 methods to access your object: directly by key, using Querystring or by key/param.

Direct access:

{
  "typicode1": {
    "name": "typicode1"
  },
  "typicode2": {
    "name": "typicode2"
  },
  "typicode3": {
    "name": "typicode3"
  }
}

Query: IP:PORT/typicode1, returns the object directly.

Using querystring:

{
  "names": [
    { "name": "typicode1" },
    { "name": "typicode1" }, // <-- Will return 2 object entries
    { "name": "typicode2" },
    { "name": "typicode3" }
  ]
}

Query: IP:PORT/names?q=typicode1, returns an array with the results.

Key/parameter:

{
  "names": [
    { "id": 1, "name": "typicode1" },
    { "id": 2, "name": "typicode2" },
    { "id": 3, "name": "typicode3" },
    { "id": 3, "name": "typicode4" } // <-- Wont be printed, unless queried with querystring.
  ]
}

Query: IP:PORT/names/1, returns the object with id 1 (Primary key). Note that /names/3 will return only the first entry (Duplicated primary keys).

To access the object in the querystring, you have to select the first element in the array that it returns. In javascript, it would be var typicode1 = JSON.parse(queriedJson[0]). I don't think this is the best way to solve this problem, because querystrings can return whatever value they match, this means that your response could have false-positives.

I recommend using the third method, as it is easier to read and is widely used (As an example, Github API. [They use the username as the primary key for queries, instead of id. Your profile: here]).

JSON-Server implements some of the HTTP verbs, so you can automatically GET/POST/PUT/DELETE values. There are also extra functionalities like filter, paginate, sort... the documentation is on the home page of the project.

Just remember that this is a fake service, use this project just as a reference for implementing your REST endpoints with a proper backend later on.

@xgqfrms-GitHub
Copy link
Author

Thanks a lot, again!

@Henrique-Alves
Copy link

cara estou com esse json, queria consumir ele na minha outra pagina, vi que consome com require() mas da erro pq tem que ser java puro e estou utilizando api, se puder dar uma força.
{
"games":[
{
"nome": "dark souls",
"tela": "src=https://sm.ign.com/t/ign_br/news/g/gamescom-2/gamescom-2015-prepare-for-dark-souls-3-with-this-g_9qgr.1280.jpg",
"anoLancamento": 2009,
"tipo": "RPG",
"nota": 9
},

    {
        "nome": "CS",
        "tela": "src=https://s2.glbimg.com/_0reXPq2B0kVJXmZwzN9vB97Fq4=/0x0:1920x1080/924x0/smart/filters:strip_icc()/i.s3.glbimg.com/v1/AUTH_bc8228b6673f488aa253bbcb03c80ec5/internal_photos/bs/2019/s/W/rPsLN6SyAp4q7AcIBYTQ/1541419312747-tr-2-wallpaper.png",
        "anoLancamento": 2012,
        "tipo": "Ação",
        "nota": 9
    },

    {
        "nome": "battlefield 5",
        "tela": "src=https://thumbs.gfycat.com/FittingFarCommongonolek-size_restricted.gif",
        "anoLancamento": 2018,
        "tipo": "Ação",
        "nota": 9
    },

    {
        "nome": "call of duty",
        "tela": "src=https://img.olhardigital.com.br/uploads/acervo_imagens/2019/10/r16x9/20191015120757_1200_675_-_call_of_duty_mobile.jpg",
        "anoLancamento": 2003,
        "tipo": "acão",
        "nota": 7
    },

    {
        "nome": "Assassin's Creed",
        "tela": "src=https://s2.glbimg.com/x5dqQybuuq4-kwc-wNuHTeH-r64=/695x0/s.glbimg.com/po/tt2/f/original/2016/11/30/assassins-creed-3-gratis-uplay-ubisoft-30-anos.jpg",
        "anoLancamento": 2012,
        "tipo": "Ação e aventura",
        "nota": 7
    },

    {
        "nome": "watch dogs",
        "tela": "src=https://s2.glbimg.com/PXSHDEX_xF43LgzNS7crxZ6lvrw=/0x0:1011x706/695x485/s.glbimg.com/po/tt2/f/original/2014/05/28/watch-dogs-001_1_1.jpg",
        "anoLancamento": 2014,
        "tipo": "Ação e aventura",
        "nota": 8
    },

    {
        "nome": "GTA 5",
        "tela": "src=https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcR_MTwtV9sVwLlf3vKolOKyerZyUqrxjZoaZ5mj2EILAdRnAi9K&usqp=CAU",
        "anoLancamento": 2013,
        "tipo": "Ação",
        "nota": 10
    },

    {
        "nome": "Red Dead Redemption 2",
       "tela": "src=https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRBAzhKB85veIGu8mB5wYkM0KQs1glFw6cS72jz7UYUdLXfIYhs&usqp=CAU",
        "anoLancamento": 2018,
        "tipo": "Ação e aventura",
        "nota": 9
    },

    {
        "nome": "God of War",
        "tela": "src=https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRZ_0uMpgLJxFs5t7YgFWeJdrzHY2cbAmD_jR49dhEM0sx1de-j&usqp=CAU",
        "anoLancamento": 2018,
        "tipo": "Ação e aventura",
        "nota": 7
    },

    {
        "nome": "Forza Horizon 4s",
        "tela": "src=https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTfGhp4jPt9R2ajVTQiQfaolAjiC0A5IHDF5-pOSy1IY8qiw_E1&usqp=CAU",
        "anoLancamento": 2018,
        "tipo": "Esportes ou Corrida",
        "nota": 8
    }
]

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants