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.

database rules validation causing Unexpected token in JSON error

See original GitHub issue

[REQUIRED] Environment info

firebase-tools: 11.0.1

Platform: Windows 10

[REQUIRED] Test case

firebase init database --debug shouldn’t throw an JSON syntax error.

[REQUIRED] Steps to reproduce

Run firebase init database --debug Choose “Use an existing project” Choose any project Use default file name for Realtime Database security rules

[REQUIRED] Expected behavior

Database init finished successfully, not with JSON syntax error

[REQUIRED] Actual behavior

? Are you ready to proceed? Yes

=== Project Setup

First, let's associate this project directory with a Firebase project. 
You can create multiple project aliases by running firebase use --add, 
but for now we'll just set up a default project.

? Please select an option: Use an existing project
[2022-05-30T12:30:16.316Z] >>> [apiv2][query] GET https://firebase.googleapis.com/v1beta1/projects pageSize=100
[2022-05-30T12:30:16.981Z] <<< [apiv2][status] GET https://firebase.googleapis.com/v1beta1/projects 200
[2022-05-30T12:30:16.982Z] <<< [apiv2][body] GET https://firebase.googleapis.com/v1beta1/projects [omitted]
? Select a default Firebase project for this directory: fir-kris-test (firebase-kris-test)
i  Using project fir-kris-test (firebase-kris-test)

=== Database Setup
i  database: ensuring required API firebasedatabase.googleapis.com is enabled...
[2022-05-30T12:30:33.150Z] >>> [apiv2][query] GET https://serviceusage.googleapis.com/v1/projects/fir-kris-test/services/firebasedatabase.googleapis.com [none]
[2022-05-30T12:30:34.083Z] <<< [apiv2][status] GET https://serviceusage.googleapis.com/v1/projects/fir-kris-test/services/firebasedatabase.googleapis.com 200
[2022-05-30T12:30:34.084Z] <<< [apiv2][body] GET https://serviceusage.googleapis.com/v1/projects/fir-kris-test/services/firebasedatabase.googleapis.com [omitted]
+  database: required API firebasedatabase.googleapis.com is enabled

[2022-05-30T12:30:34.090Z] >>> [apiv2][query] GET https://firebasedatabase.googleapis.com/v1beta/projects/fir-kris-test/locations/-/instances/fir-kris-test-default-rtdb [none]
[2022-05-30T12:30:34.487Z] <<< [apiv2][status] GET https://firebasedatabase.googleapis.com/v1beta/projects/fir-kris-test/locations/-/instances/fir-kris-test-default-rtdb 200
[2022-05-30T12:30:34.489Z] <<< [apiv2][body] GET https://firebasedatabase.googleapis.com/v1beta/projects/fir-kris-test/locations/-/instances/fir-kris-test-default-rtdb {"name":"projects/4564564564/locations/europe-west1/instances/fir-kris-test-default-rtdb","project":"projects/4564564654","databaseUrl":"https://fir-kris-test-default-rtdb.europe-west1.firebasedatabase.app","type":"DEFAULT_DATABASE","state":"ACTIVE"}

Firebase Realtime Database Security Rules allow you to define how your data should be
structured and when your data can be read from and written to.

? What file should be used for Realtime Database Security Rules? database.rules.json
[2022-05-30T12:31:02.589Z] >>> [apiv2][query] GET https://fir-kris-test-default-rtdb.europe-west1.firebasedatabase.app/.settings/rules.json [none][2022-05-30T12:31:02.772Z] <<< [apiv2][status] GET https://fir-kris-test-default-rtdb.europe-west1.firebasedatabase.app/.settings/rules.json 200
[2022-05-30T12:31:02.773Z] <<< [apiv2][body] GET https://fir-kris-test-default-rtdb.europe-west1.firebasedatabase.app/.settings/rules.json "{\n  /* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */\n  \"rules\": {\n    \".read\": false,\n    \".write\": false\n  }\n}"

Error: Unable to parse JSON: SyntaxError: Unexpected token / in JSON at position 4

So yea, comment in JSON file breaks the flow. First time using firebase

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kristiansaarelacommented, May 31, 2022

I agree that some kind of improvements should be applied. Specially since it was 2nd command in Firebase documentation (firebase init), and it failing by default does not provide confidence.

I’ve removed comments from rules in Firebase console (web) and it works now.

Thank you for helping.

1reaction
macksalcommented, May 31, 2022

Ah, it is downloading the rules that already exist in the project, and these include comments by default.

I’m able to replicate this by adding comments through the web interface. An easy workaround is just to remove the comment from the RTDB rules editor in the Firebase console (web).

It still probably represents an oversight on Google’s part to allow comments to be added in the web interface, then expect the downloaded file to be valid JSON, so it likely deserves a fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Is JSON and How to Handle an “Unexpected Token” ...
The first thing to do in this situation is to confirm where the error is happening exactly. To ensure the error happens on...
Read more >
SyntaxError: Unexpected token < in JSON at position 0
I experienced this error "SyntaxError: Unexpected token m in JSON at position", where the token 'm' can be any other characters.
Read more >
How to Fix SyntaxError: Unexpected token < in JSON at ...
Usually this error is caused when your server returns HTML (which typically begins with <DOCTYPE html> or <html> ) instead of JSON. Valid...
Read more >
Vlocity Deployment Returns Error Unexpected Token in ...
How to workaround the issue: Let's take the error above. The error means that 1 corrupted JSON files was found in the promotion...
Read more >
syntaxerror unexpected token is not valid json
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data. JSON Parse error: Unrecognized token '<' Summary. These errors indicate...
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