Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a custom policy (studying for a certification) and was trying to create a comment within the JSON code block.

Any assistance would be appreciated. Thank you!

This is what I attempted after doing research, as there is very little regarding the ERROR AWS threw regarding the comments.

1. This is the code block to be used as an example from AWS:
-------------------------------------------------------------
{
   "Version": "2012-10-17",
   "Statement": [
   {
      "Effect": "Allow",
      "Action": "support:*",
      "Resource": "*"
   },
   {
       "Effect": "Deny",
       "Action": "support:ResolveCase",
       "Resource": "*"
    }]
}


2. This is the modified code with JSON comments (which threw the ERROR):
------------------------------------------------------------------------
{
    "__comment1__": "Custom policy to specify what actions to allow or deny.",
    "__comment2__": "The following policy statement allows an IAM user to perform",
    "__comment3__": "all actions in AWS Support except resolve a case.",
    "Version": "2012-10-17",
   "Statement": [
   {
      "Effect": "Allow",
      "Action": "support:*",
      "Resource": "*"
   },
   {
       "Effect": "Deny",
       "Action": "support:ResolveCase",
       "Resource": "*"
    }]
}


The ERROR AWS threw is as follows:
Ln 2, Col 20Invalid Policy Element: The policy element __comment1__ is not valid.


What I have tried:

I have tried to add in comments, per the article from FreeCodeCamp but that is when AWS threw the ERROR.

JSON Comment Example — How to Comment in JSON Files[^]
Posted
Updated 23-Jan-23 0:30am

JSON is a data interchange format and comments are not part of JSON syntax. JSON parsers will not consider them and will only parse the data. So, comments are not officially supported but you can use an alternative like use a non-JSON format that supports comments, such as YAML, and then convert it to JSON.

However, you can still use comments in JSON files as custom elements like:
JSON
{
  "name": "John",
  "age": 30,
  "comments": "This is a sample comment"
}
 
Share this answer
 
v4
There is no such thing as a comment in JSON. It is a data formatting specification, not code.

Whether or not that "comment" is actually parsable depends on the JSON parser implementation being used to load the data. If the example you linked, the JSON parser in Python is being used to load the file. That parser does not see the "_comment" as a real comment. It's being loaded as just another string in the resulting data object.

Some parsers will support loading this, but keep in mind that every one of them is going to treat the comment as part of the data. It's NOT going to be ignored.

Other implementations require a class to be defined to deserialize the data into. If the parser does not ignore data in the JSON file that it cannot find a field in the target class for, you can get errors like you're running into.
 
Share this answer
 
v3
As mentioned by others here, it is not part of the spec. However, there is a raised request that you can monitor to see if there will be any planned support: [System.Text.Json] Add ability to serialize Comments on Properties · Issue #35251 · dotnet/runtime · GitHub[^].
 
Share this answer
 
Quote:
Ln 2, Col 20 Invalid Policy Element: The policy element __comment1__ is not valid.
That's an error generated by whatever service you're calling. It indicates that they are enforcing a specific JSON schema[^], which means that you cannot simply add new unknown elements to the document.

You will need to study the schema provided for the service you're calling to see whether they provide fields for comments. If they don't, then you won't be able to include comments in your JSON payload.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900