Skip to content
Advertisement

Adding json value in yml in python changing the format to yml again

I have to fetch json data and paste it in one of the values in yml. But it is converting it to yml format.

JavaScript
JavaScript
JavaScript

I want the output as below

JavaScript

but with the above script it is showing all the data in dictionary format format. I would like to replace the sampleyaml with the added changes.

Please help me.

Advertisement

Answer

What you have at the end of your YAML document, indicated by >, is called a folded scalar string (and in your input file it is empty). So you want to assign the contents of the JSON file as a string and not as a data structure, and use a more modern library than PyYAML that can easily output/preserve such strings out of the box.

Since you also parse the contents of the JSON file for the slug value, you should read in its contents once in a string and parse that:

JavaScript

which gives:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement