Skip to content

json-server Converts Numeric "id" Fields to Strings #1636

Open
@RushiLee

Description

@RushiLee

Steps-
1.Create a JSON file (Ninja.json) with the following content
{
"Ninjas": [
{
"UID": 1,
"name": "Neji",
"courses": ["Maths", "Science"],
"ContactNum": 1111,
"Location": "Mumbai"
},
{
"UID": 2,
"name": "RockLee",
"courses": ["Magic", "Science"],
"ContactNum": 2222,
"Location": "Goa"
},
{
"UID": 3,
"name": "Garaa",
"courses": ["Geo", "Science"],
"ContactNum": 2222,
"Location": "Goa"
}
]
}

2.Start json-server with
json-server Ninja.json

3.Perform a GET request to http://localhost:3000/Ninjas

Expected Behavior
{
"Ninjas": [
{
"id": 1,
"name": "Neji",
"courses": ["Maths", "Science"],
"ContactNum": 1111,
"Location": "Mumbai"
},
{
"id": 2,
"name": "RockLee",
"courses": ["Magic", "Science"],
"ContactNum": 2222,
"Location": "Goa"
},
{
"id": 3,
"name": "Garaa",
"courses": ["Geo", "Science"],
"ContactNum": 2222,
"Location": "Goa"
},
{
"id": 4,
"name": "Naruto",
"courses": ["Hindi", "Maths"],
"ContactNum": 2222,
"Location": "Goa"
},
{
"id": 5,
"name": "Sasuke",
"courses": ["History", "Economics"],
"ContactNum": 2222,
"Location": "Goa"
}
]
}

Actual Behavior
[
{
"id": "1",
"name": "Neji",
"courses": [
"Maths",
"Science"
],
"ContactNum": 1111,
"Location": "Mumbai"
},
{
"id": "2",
"name": "RockLee",
"courses": [
"Magic",
"Science"
],
"ContactNum": 2222,
"Location": "Goa"
},
{
"id": "3",
"name": "Garaa",
"courses": [
"Geo",
"Science"
],
"ContactNum": 2222,
"Location": "Goa"
},
{
"id": "4",
"name": "Naruto",
"courses": [
"Hindi",
"Maths"
],
"ContactNum": 2222,
"Location": "Goa"
},
{
"id": "5",
"name": "Sasuke",
"courses": [
"History",
"Economics"
],
"ContactNum": 2222,
"Location": "Goa"
}
]

Workaround:
Changing id to a different field name (e.g., uid) prevents this behavior

Additional Details-
C:\Windows\System32>npm --version
10.9.2

C:\Windows\System32>node --version
v22.14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions