Creation of JSON with numeral

Asked

Viewed 83 times

-2

I would like to create a JSON like this (this is only a part), I always create the object with the value, but I can’t do it this way using the 200 as an object:

path.get.response = new Object()
path.get.response.200 = new Object()

  "responses": {
      "200": {
        "description": "Operação realizada com sucesso!",
        "content": {
          "application/json": {
            "schema": {

....

  • path.get.response.["200"] = new Object()

  • @Interesting Isac, this way you convert 200 to string right? Only later to access this object is only that way also right? path.get.response.["200"]

  • Yes, however I made a mistake and put a . the more. It’s just path.get.response["200"]

  • didn’t work that way friends...

1 answer

0

Is it really necessary to name the object as 200? I believe that objects cannot start with number, hyphenate or special character. Maybe the ideal would be for you to call it status200 or S200, ultimately depends on your creativity.

An object property name can be any string Valid Javascript, or anything that can be converted into a string, including an empty string. However, any name and property that is not a valid Javascript identifier (for example, a property name that has a space or a hyphen, or starts with a number) can only be accessed(a) using the notation of square brackets [...]

You can check out more in this article - Working with Objects

  • According to the json example the object name should be 200, because it is the json definition, but if you can’t do it with object and value, I will have to find another alternative.

  • The API returns the object to you or this JSON is local?

  • I am generating this json, on top of existing patterns, I am making a generator, where I type the information and the generator generates the json, but I think I will generate with another name instead of the 200 and when generating the file I rename to 200.

  • Got it, there’s the way @Isaac showed it too path.get.response.["200"], from what I understand this way you convert 200 to string, it would be a way to get around this problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.