🐞 it seems that there is a bug ?
See original GitHub issue🐞
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"
}
]
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top Results From Across the Web
Miraculous Ladybug | Theme Song Music Video 🐞 ft. Lou ...
Check out this awesome cover of the opening to Miraculous Ladybug starring Lou & Lenni-Kim! Ladybug and Cat Noir are there in Paris...
Read more >LADYBUG & CAT NOIR 💥 | Miraculous - Compilation Season 2
🐞 LADYBUG & CAT NOIR 💥 | Miraculous - Compilation Season 2. Watch later. Share. Copy link. Info. Shopping. Tap to unmute.
Read more >Antibug 🐞 | Ladybug and Cat Noir | Animation - YouTube
Miraculous Ladybug | 🐞 Antibug 🐞 | Ladybug and Cat Noir | Animation ... They each have a Miraculous, a jewel linked to...
Read more >MIRACULOUS - Akumatized 🐞 | Tales of Ladybug and Cat Noir
They each have a Miraculous, a jewel linked to their the magical creatures (Kwamis) that give them powers. Their mission is to keep...
Read more >LADYBUG & CAT NOIR 💥 | Miraculous - Compilation Season 3
🐞 LADYBUG & CAT NOIR 💥 | Miraculous - Compilation Season 3. Watch later. Share. Copy link. Info. Shopping. Tap to unmute.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can use 3 methods to access your object: directly by key, using Querystring or by key/param.
Direct access:
Query:
IP:PORT/typicode1
, returns the object directly.Using querystring:
Query:
IP:PORT/names?q=typicode1
, returns an array with the results.Key/parameter:
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.
In the first sample, you are declaring a single object
profile
with 3 key-value pairsname
. 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:
There’s a collision on the
property
key.Using an array:
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