statiqdev / Statiq.Web

Statiq Web is a flexible static site generator written in .NET.
https://statiq.dev/web
Other
1.64k stars 235 forks source link

How to read a json file with multiple records? #957

Closed dotnetshadow closed 2 years ago

dotnetshadow commented 2 years ago

Hi there,

Firstly a great effort on this project.

I would like to read a json file with multiple records, I've been successful when it's a single item, but I just can't seem to read in when there are multiple items such as below:

Multiple items

{
    "fruits": [
        { 
        "Name": "Apple", 
        "Id": 1
        },
        { 
            "Name": "Banana", 
            "Id": 2
        }
    ]
}

Single item

{ 
        "Name": "Apple", 
        "Id": 1
}

This is how I'm reading in the files

@{ IDocument[] fruits = Outputs
              .FromPipeline("Data")
              .FilterSources("food/fruit/*")
              .ToArray(); }

.cshtml page

@foreach (var fruit in fruits) {
    var name = fruit.GetString("Name") // only works for single items
}

Basically I would like one of my pages in razor to loop over those fruits and display them on the page, whether it's an array of items or a single item

Questions:

  1. Could you please provide some guidance on how to go about it?
  2. I noticed there is a csvreader in the beta, do you have an example or documentation on how to use that?

Thanks for your time and effort

daveaglick commented 2 years ago

Sorry for the extremely late reply on this 😬

The trick is probably going to be to get nested complex objects as documents. See the answer in this discussion: https://github.com/statiqdev/Discussions/discussions/22

Since this issue is so old, I'll go ahead and close it with the assumption that gets you where you need to go. Please feel free to comment and I can reopen if that's not the case.