Replacing data in Yaml file

I am working on a freelance project. I have a Yaml file that I want to convert to JSON. But before I convert it to JSON, I need to remove the first line and anything on that line.

data.yml file (“data:” needs to be removed):

data:
    name: XYZ Company
    location: Portland, OR
    services:
        development: yes
        design: no

The edited Yaml data file I need to convert to JSON:

        name: XYZ Company
        location: Portland, OR
        services:
            development: yes
            design: no

The reason I need to do this is because I don’t actually need the first part of the Yaml file (I only use it for Netlify CMS collections config so my client can edit the data file).

Here’s what I have, but Middleman doesn’t work and gives me an undefined method error:
<%= YAML.load_file('data/data.yml').sub('data','').to_json %>

Any thoughts as to how I can achieve this?

This should get you the right json: <%= data.data.data.to_json %> Having a data file in a data folder with a data object doesn’t exactly lead to the most semantic query but it works lol. Netlify CMS can edit YAML just fine by the way, it doesn’t need a JSON formatted file. You could also use the object widget to nest your content in an object.

1 Like

Yep that’s exactly what I’m doing in Netlify CMS - Yaml file and an object widget. Sorry I don’t think I was clear. I’m using the Yaml file in Netlify CMS only. That’s the file my client updates.

I then need the data file converted to JSON so that it works with the third-party library I’m using (using SurveyJS library and it needs a a specific JSON format for the question data).

Because Netlify CMS config requires a name field for the collection file, I had to wrap the Yaml file with a name so I could choose specific fields for editing. But when I convert that Yaml file to JSON for SurveyJS, it breaks because it has a specific format it requires and it doesn’t recognize the name used purely for NetlifyCMS.

You only need lists to be named in Netlify CMS, you can totally produce the content without the data object in a file collection. I’ll post a config later on, since I’m away from my computer.

collections:
  - name: static
    label: Static
    files:
      - file: "data/data.yml"
        label: Data
        name: data
        fields:
          - {name: name, label: Name, widget: string}
          - {name: location, label: Location, widget: string}
          - name: services
            label: Services
            widget: object
            fields: 
              - {name: development, label: Development, widget: boolean}
              - {name: design, label: Design, widget: boolean}
1 Like

Tom this is amazing. Thank you! Your responses were very helpful and got me to where I need to be. Appreciate you.

1 Like