question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

🐞 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:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
arthur-melocommented, 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.

2reactions
arthur-melocommented, Apr 10, 2017

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found