JSON to Markdown converter and vice versa

Is there any easy way to convert markdown to JSON or JSON to markdown? I am talking in the context of Rasa training data format.

I googled it but didn’t get appropriate results.

You can use rasa-nlu converter code for this. You can take a look at TrainingData class in training_data.py file, which has some methods to convert the training data format. For ex:

    def as_markdown(self):
        # type: () -> str
        """Generates the markdown representation of the TrainingData."""
        from rasa_nlu.training_data.formats import MarkdownWriter
        return MarkdownWriter().dumps(self)

    def as_json(self, **kwargs):
        # type: (**Any) -> str
        """Represent this set of training examples as json."""
        from rasa_nlu.training_data.formats import RasaWriter
        return RasaWriter().dumps(self)

So, you could use these functions in your own code to convert. For ex -

from rasa_nlu.training_data import load_data
_input = "<input-file-path>" #converting the json format to md format
_output = "<output-file-path>" #new file of md format
with open(_output,'w') as f:
    f.write(load_data(_input).as_markdown())
3 Likes