#community-help

Resolving "Bad JSON" Error during Typesense Collection Creation

TLDR Cassandra had an error creating a Typesense collection. Kishore Nallan resolved this by suggesting to input JSON data directly, not as a file.

Powered by Struct AI
9
17mo
Solved
Join the chat
Apr 05, 2022 (17 months ago)
Cassandra
Photo of md5-3cbb7fe1dc4e108829c749183bbecfb4
Cassandra
07:49 AM
Hello, I have a problem when I try to create a collection the following error is returned: {"message": "Bad JSON."} Yet I made a copy and paste of the official Typesense guide.
07:50
Cassandra
07:50 AM
curl "http://workmetadata.francecentral.cloudapp.azure.com:5005/collections" -X POST -H "Content-Type: application/json" -H "X-TYPESENSE-API-KEY: keyhdh" -d collection.json
07:51
Cassandra
07:51 AM
Here is the command line I run and my file collection.json
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:54 AM
šŸ‘‹ Can you please post the collection.json as text here? Let me try, maybe there are some non-visible bad characters that's not valid json.
Cassandra
Photo of md5-3cbb7fe1dc4e108829c749183bbecfb4
Cassandra
07:56 AM
{
"name": "books",
"fields": [
{"name": "title", "type": "string" },
{"name": "authors", "type": "string[]", "facet": true },

{"name": "publication_year", "type": "int32", "facet": true },
{"name": "ratings_count", "type": "int32" },
{"name": "average_rating", "type": "float" }
],
"default_sorting_field": "ratings_count"
}
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:59 AM
This works for me locally:

curl -k "" -X POST -H "Content-Type: application/json" \
      -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '{
    "name": "books",
    "fields": [
      {"name": "title", "type": "string" },
      {"name": "authors", "type": "string[]", "facet": true },
      {"name": "publication_year", "type": "int32", "facet": true },
      {"name": "ratings_count", "type": "int32" },
      {"name": "average_rating", "type": "float" }
    ],
    "default_sorting_field": "ratings_count"
  }'
08:00
Kishore Nallan
08:00 AM
The guide uses inline JSON bodies like this and not -d collection.json -- if you can tell me where exactly in the guide we use this format, I can also check that.
Cassandra
Photo of md5-3cbb7fe1dc4e108829c749183bbecfb4
Cassandra
08:02 AM
I copy paste your command line and it works. Thank you very much
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
08:02 AM
šŸ‘