How do I represent a GUID in a json object?
Asked Answered
C

4

33

For GUID type in JSON, do we need double quote like

"Id": "9903ED01-A73C-4874-8ABF-D2678E3AE23D" 

or just like

"Id" : 9903ED01-A73C-4874-8ABF-D2678E3AE23D
Clothbound answered 19/11, 2010 at 23:39 Comment(0)
R
51

There is no such thing as a 'GUID Type.'

You need to put it in quotes. A GUID is just a string, and string need quotes.

All of the types are listed here on the JSON website. (Look on the right side)

Rigby answered 19/11, 2010 at 23:42 Comment(1)
if I could've upvoted this again, I would've!Lucretialucretius
M
6

Guid Should be passed with quotes

"Id" : "9903ED01-A73C-4874-8ABF-D2678E3AE23D"

Masterpiece answered 23/3, 2016 at 10:23 Comment(5)
I'd be curious to know how you came up with that. There's no such thing as a GUID in JSON anymore than you have Point or Polygon, to pick two examples. To store types in JSON you have to use strings, numbers, booleans, arrays of those types, or object structures containing those types.Aperient
Yes, Json not have Guid type, but if you post above string in json, in C# it will convert into Guid variable in deserialization.Masterpiece
Rick O'Shea, I think that you downvoted this and lashed out at a developer who actually knew more about this subject than yourself. You misunderstood the answer and downvoted, but that's not Ravindra's fault. Ravindra is saying that to pass a STRING to be serialized to a guid to an endpoint, INSIDE OF YOUR JSON, you make sure to wrap your string with quotes. That's the entire point of this Question by the OP.Horsepower
The answer originally said to pass it without quotes, which is what Rick O'Shea was responding to. Ravindra corrected the answer afterwards.Heard
Useful reference from NewtonSoft: newtonsoft.com/json/help/html/SerializationGuide.htmStocking
G
0

Simple {"a241306c-235d-4cab-a728-a18236844776"} worked for me.


I was trying to deserialize JSON into a class

public class EraseData
{
    public int Size { get; set; }

    public IEnumerable<Guid> EnterpriseIds { get; set; }
}

and this was valid JSON syntax.

{
  "Size": 10,
  "EnterpriseIds": [
    "a241306c-235d-4cab-a728-a18236844776",
    "977fbb1d-5d3f-47a5-a33d-0762ef5d9ac7"
  ]
}
Gladdie answered 11/11, 2021 at 20:5 Comment(0)
T
-1

Guid is treated as a string so you should go with "9903ED01-A73C-4874-8ABF-D2678E3AE23D"

Thacher answered 5/4, 2023 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.