#community-help

Fixing "bad json" error in Typesense 0.24

TLDR Adam experiences "bad json" error when creating collections in Typesense 0.24. Jason identifies a typo in the docs causing the error and helps troubleshoot additional errors.

Powered by Struct AI

3

1

1

23
9mo
Solved
Join the chat
Mar 09, 2023 (9 months ago)
Adam
Photo of md5-d1ca9f479dc9860becc380edf02d7689
Adam
07:44 PM
hi everyone - I’m wondering if someone can help me understand a problem I’m running into trying to create collections with a schema which has object as the type on a field. I’m using typesense 0.24 on docker. even when I try copying and pasting the secondcurl example, the server errors and tells me I have “bad json”.
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:46 PM
Could you adapt this gist, to replicate the error you’re seeing, so I can try it out locally?
Adam
Photo of md5-d1ca9f479dc9860becc380edf02d7689
Adam
07:46 PM
let me take a look and see what I can do. one moment please :simple_smile:

1

07:52
Adam
07:52 PM
oh this is interesting - when I try the first example on the docs page, with the auto indexed field, the collection is created successfully
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:53 PM
Looks like there’s a typo in the docs. The last trailing comma after the array of fields is what’s causing the Bad JSON error
07:53
Jason
07:53 PM
I’ll fix the docs

1

07:54
Jason
07:54 PM
This should work

curl -k "" -X POST -H "Content-Type: application/json" \
      -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '{
        "name": "docs",
        "enable_nested_fields": true,
        "fields": [
          {"name": "person", "type": "object"},
          {"name": "details", "type": "object[]"}
        ]
      }'
Adam
Photo of md5-d1ca9f479dc9860becc380edf02d7689
Adam
07:54 PM
ah ok let me try that and then I’ll try my own project again. perhaps I missed a comma or something
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:54 PM
The docs have an extra comma after "object[]"},, which is wrong
Adam
Photo of md5-d1ca9f479dc9860becc380edf02d7689
Adam
07:54 PM
ok yes, that worked. one moment while I check the schema for my project…
07:56
Adam
07:56 PM
oh no…. I’m feeling really silly… I wrote the schema in a ts file. I just have a habit of adding trailing commas to objects. that must be messing up the JSON object sent to the server. I’ll try removing them and see what happens
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
07:59 PM
I’ve done this too 🙂
08:00
Jason
08:00 PM
Sadly the JSON spec doesn’t allow trailing commas, even though Typescript, ECMAscript spec allow for it. So JSON parsers are strict about it
Adam
Photo of md5-d1ca9f479dc9860becc380edf02d7689
Adam
08:00 PM
yeah and I also forgot about using double quotes instead of single quotes… habit

1

08:01
Adam
08:01 PM
well the collection got created and then I got a new kind of error. so that’s progress!
08:01
Adam
08:01 PM
fwiw I’m using a proxy API to create collections, so there’s likely a problem with that
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:01 PM
Btw, this is only an issue if you use curl. If you’re using the JS/TS client library, you can just pass in a JS object and the library will take care of converting it to JSON
Adam
Photo of md5-d1ca9f479dc9860becc380edf02d7689
Adam
08:03 PM
ah right so my thing is a little different. I’m building a WP plugin which is passing the data through a nextJS proxy to the typesense server. it’s a little roundabout, but it’ll help with my situation and security. I think…
08:08
Adam
08:08 PM
this is weird…. the JS client says the collection got created, but it also can’t be found…. I’ll keep working on it. it must be something I did
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:09 PM
If you’re trying to read a collection right after it was created in successive method calls, you might see eventual consistency issues. Since it takes about a second for the collection to be available in memory for a get / search call
Adam
Photo of md5-d1ca9f479dc9860becc380edf02d7689
Adam
08:11 PM
hm ok that’s good to know. I’ll check that out too. thanks for your help!

1

10:47
Adam
10:47 PM
well in the end it turns out I had one last problem that I’d overlooked. at one point, one of the fields was typed with a string and I’d set sort to true. I had accidentally changed the type to object but overlooked removing the sort property

1

Typesense

Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI

Indexed 3011 threads (79% resolved)

Join Our Community