#Sourcing Data from JSON with Content Collection failing
1 messages Β· Page 1 of 1 (latest)
In that case, are you saying to take the bio strings, turn those into objects with arbitrary ID values, so something like:
...,
"bio": {
"professional": {"id": 1, "text": "a string" },
"personal": {"id": 2, "text": "another string" },
}
pretty much
let me see
or perhaps the ID key should be on "bio"
removing console log does not change error
i dont work with content collections much
so bio would have the ID and your schema would look like
schema: z.object({
id: z.string(),
professional: z.string(),
personal: z.string(),
}),
Indeed, I have that open as well
I'll be so grouchy if this works
The ID info can be found here https://docs.astro.build/en/guides/content-collections/#built-in-loaders
Perhaps we need to add clarification to the nested json section as well if this does help
darn
you need to use a string for it
bio β id data does not match collection schema.
Expected type "object", received "number"
hmm that's weird
also
do you have a repo I could look at?
I don't have this pushed, it's a private repo, and I'm not SUPER fluent with git
Let me see if I can get a minimal repro
That's something I need to get better at
Sure, much appreciated
I am much more of a "try things and see if it works" debugger
Not very good at talking others through my convuluted thought process π
That's all good. Are you an astro contributor/maintainer?
Just a helpful community member π
(Though I have contributed a small amount to the docs)
β€οΈ
I would love to contribute but I think I don't know enough about the lower-level details
You are actually earning contributor credit just by being active in the discord π
One of the great things about the Astro team is they really recognize the power and benefits of having a strong welcoming community, and so just by being a part of that you will eventually recieve the contributor tag in discord automatically π
It seems making the data an array works
So it doesn't like:
"bio": {
"professional": "string",
"personal": "another string"
}
but it DOES like:
"bio": [
{
"id": 1,
"professional": "string",
"personal": "another string."
}
]
Which will work but it's sort of annoying (to me)
I guess it makes sense cause this isn't really a collection per se, I guess the word collection implies an array-like structure. The docs seem to suggest that you can source stuff from a plain old JSON object, but for some reason I can't seem to get it working.
Man what a good catch!
I didn't think of that because your work-experience is an array so I just assumed the latter was without really looking π
Yeah the collection would have to be an array of objects
Yeah i mean if I was making inferences I would think that being able to source data from an OBJECT would be a pretty standard use case
I was even getting an error message that says it needs either to be an array OR an object
From their docs:
The file() loader creates entries from a single file that contains an array of objects with a unique id field, or an object with string keys. It supports JSON or YAML, and you can provide a custom parser for data files it cannot parse by default.
I think the idea is that if you just have a single object, why not just import that object vs using a collection, since a collection by it's nature needs to be multiple things
yeah good call, I saw some docs about Vite allowing you to directly import JSON objects
But that is interesting that you can use a top level object but not a nested object!
maybe that's a better thing to do in my use case
I went ahead and asked the docs team if we need to clarify that nested json needs to be structured in an array of objects on that particular page π
Thank you - please @ me if you need more testing/info. I can probably make a repo if it would be helpful.
I might work on that right now real quick
I think this should represent a working (as in not working) example of the issue I'm facing:
@thorn kernel π
If you get a chance would you mind checking out Armands suggested reworks to the docs to clarify the file loader file structure?
Feel free to leave a comment π
https://github.com/withastro/docs/issues/11027#issuecomment-2676889444
π Subject area/topic Content Loader API π Page(s) affected (or suggested, for new content) https://docs.astro.build/en/reference/content-loader-reference/#file-loader π Description of content that ...
Hey! I think that I agree with you, the first rewrite they suggested seems to be clear. What I found was that you can just import a JSON file directly into an Astro file and treat the import as an object. My only other thought is that this - to me at least - seems like a common enough use case that it might get more mention somewhere within the content collection documentation. It seems like there could almost be a section in the docs aimed at the various ways you can source data. For my use case it makes more sense to just import the JSON file directly and my confusion was around the idea that content collections are specifically for list-type data. Iβm still relatively new to Astro though so I attribute this to my lack of experience. Iβve read certain parts of the docs pretty closely but by no means all parts.
Iβve been reading a bit more in the documentation and itβs definitely fair to say that this could just be an issue with how I interpreted the existing verbiage - donβt want to make too much work for anyone else
Feel free to drop a comment on the issue π