Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use replace holder in json with python code?

I have a question to update json script with replace holder with python.

The code:

json_str = '''
<Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
                  <Statement>
                  <DatabaseName>{database}</DatabaseName>
                    <![CDATA[
                    {{
                      \"create\": {{
                        \"object\": {{
                          \"database\":\"{{{database}}}\"
                        }}
                        }}
                      }}
                    ]]>
                </Statement>
        </Execute>'''

replaced_str = json_str.format(database='testDB')
print(replaced_str)

The output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 <Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
                                  <Statement>
                                  <DatabaseName>testDB</DatabaseName>
                                        <![CDATA[
                                        {
                      "create": {
                        "object": {
                          "database":"{testDB}"
                        }
                        }
                      }
                                        ]]>
                                </Statement>
                </Execute>

Anybody is familiar about how to change the curly brace escape to get the expected output from:
"database":"{testDB}" to "database":"testDB" ?

Thanks

>Solution :

Remove two of {}: From {{{database}}} to {database}:

json_str = '''
        <Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
            <Command>
                  <Statement>
                  <DatabaseName>{database}</DatabaseName>
                    <![CDATA[
                    {{
                      \"create\": {{
                        \"object\": {{
                          \"database\":\"{database}\"
                        }}
                        }}
                      }}
                    ]]>
                </Statement>
            </Command>
            <Properties />
        </Execute>'''
replaced_str = json_str.format(database='testDB')
print(replaced_str)

Output:


        <Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
            <Command>
                  <Statement>
                  <DatabaseName>testDB</DatabaseName>
                    <![CDATA[
                    {
                      "create": {
                        "object": {
                          "database":"testDB"
                        }
                        }
                      }
                    ]]>
                </Statement>
            </Command>
            <Properties />
        </Execute>

In fact, you can also replace \" by ".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading