Accessing JSON data files with extra dots in file names

I have a list of data files I’m copying into my /data (specifically /data/data_files) folder that have an extra dot in their file names, like so:

prices.full.json

I was previously able to use the following to pass the item name in and find the corresponding data file in my data folder.

{{ $name := .Get "name" }}
{{ $data := index .Site.Data.data_files $name }}

Which would find something like prices.json if I passed in the name prices.

However, now that my set of files has .full added to it, it’s breaking my call. So for example, the data file is now called prices.full.json.

Is there a specific way I should alter my index call to access these now?

I have run into the same thing, even when not using index.

For example, given a data file named file.json, and a copy of that file named file.foo.json:

This works:

{{ range := .Site.Data.file }}

But this doesn’t:

{{ range := .Site.Data.file.foo }}

Edit: from the data templates docs, this is why things are the way they are:

The keys in the map created with data templates from data files will be a dot-chained set of path, filename, and key in file (if applicable).

@bradbice so I think picking another naming convention is the way to go. Such as

prices_full.json

Or

pricesFull.json

Yeah, I’m hoping that’s not the only option because these files are coming from elsewhere and I don’t have control over their names.

Really hoping I don’t have to rewrite the file names with Gulp or something each time I import them.

Update:

So far I am rewriting the file names with Gulp using the following:

gulp.task('copy-tokens', ['clean'], function() {
    gulp.src(['source/*.json'])
        .pipe(
            rename(function(path) {
                path.basename = path.basename.replace('.full', '');
                return path;
            })
        )
        .pipe(gulp.dest('data/data_files/'));

It works fine, but as an extra step it’s making me :frowning:

This works for me

{{ $file := .Params.datafile}}{{ $data := getJSON $file }}
{{ range sort $data.items "title"}}

{{end}}

with

+++
<snipped>
datafile    = "/assets/links/my.links.copy.json"
+++

If you use /assets, the file will not copied to your web site

Assuming you pass “prices.full” in your name parameter, I cannot see how your code should not work …? It could be a bug, but I doubt it.

@bep That’s true, I could try that.

I was attempting this way because I was hoping to edit just one shortcode file rather than dozens of individual content pages with shortcode calls, all with just the same .full added…

Looking at this again, I was mistaken. Sorry about that.

Using index for a data file with an extra dot in the name does indeed work. Here’s my test case:

A data file at data/file.foo.json with these contents:

{
  "product_1": "10.00",
  "product_2": "20.00",
  "product_3": "30.00"
}

Then in the template:

{{ $name := "file.foo" }}
{{ $data := index .Site.Data $name }}
{{ range $key, $value := $data }}
  {{ $key }} = {{ $value }}<br>
{{ end }}

@bradbice for your shortcode name param, are you passing "prices.full.json" or "prices.full" ?

I’m only passing in "prices", in case the data format changes someday, and since I should only have to pass through the content, not the file format. And it worked just fine using index, but now it’s looking for prices.json and only finding prices.full.json and returning null.

I could change what I’m passing, but I’m doing this on multiple pages and wanted to only update the shortcode, as I mentioned above.

If I’m understanding you correctly, since your data filename has changed, you now need to pass "prices.full" instead of "prices"

Yes :slight_smile:

For now I’m instead just changing file names via Gulp.