Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.2 kB
3
Indexable
Never
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.io.IOException;

@SpringBootApplication
public class YamlToJsonConverter implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(YamlToJsonConverter.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        if (args.length != 1) {
            System.out.println("Please provide the path to the YAML file as an argument.");
            return;
        }

        String filePath = args[0];
        convertYamlToJson(filePath);
    }

    private void convertYamlToJson(String filePath) throws IOException {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        Object data = mapper.readValue(new File(filePath), Object.class);

        String json = new ObjectMapper().writeValueAsString(data);
        System.out.println("Converted JSON:");
        System.out.println(json);
    }
}
Leave a Comment