0

How can I make a string from json text when the json text contains many, many quotation marks and string escapes?

For example, the following works:

json_string = """
{
        "styles":[
                {
                        "label":"Style",
                        "target":{
                                "label":"Target"
                        },
                        "overrides":{
                                "materialProperties":{
                                        "CRYPTO_ID":{
                                                "script":{
                                                        "binding":"name"
                                                }
                                        }
                                }
                        }
                }
        ]
}

"""

However this does not, due to the escapes:

new_string = """
{
        "styles":[
                {
                        "label":"Style",
                        "target":{
                                "label":"Target",
                                "objectName":"*"
                        },
                        "overrides":{
                                "materialProperties":{
                                        "perObj":{
                                                "script":{
                                                        "code":"cvex myFn(string myObj=\"\"; export string perObj=\"\") { perObj = myObj; }  ",
                                                        "bindings":{
                                                                "myObj":"myObj"
                                                        }
                                                }
                                        }
                                }
                        }
                }
        ]
}
"""

Is there a smart way to break this up? I've had no luck breaking it out into chunks and re-assembling to form the same thing when joined and printed.

3
  • 1
    Where does the new_string come from? Did you read it from a file? (And then it is not a valid JSON string.) Or did you generate it? If the string is "as-is", you can get some luck with ast.literal_eval(new_string). Commented Aug 28, 2018 at 7:09
  • What do you mean by "does not work"? I can copy&paste&"execute" the second string just fine. Or are you saying that new_string = """ is also part of the file? Commented Aug 28, 2018 at 7:11
  • @MikeScotty But you cannot read it as JSON with json.loads. Commented Aug 28, 2018 at 7:12

2 Answers 2

2

Your string per se is valid JSON, however Python still sees the \ as special characters.

Use a raw string by prefixing your string with r:

import json

new_string = r"""
{
        "styles":[
                {
                        "label":"Style",
                        "target":{
                                "label":"Target",
                                "objectName":"*"
                        },
                        "overrides":{
                                "materialProperties":{
                                        "perObj":{
                                                "script":{
                                                        "code":"cvex myFn(string myObj=\"\"; export string perObj=\"\") { perObj = myObj; }  ",
                                                        "bindings":{
                                                                "myObj":"myObj"
                                                        }
                                                }
                                        }
                                }
                        }
                }
        ]
}
"""

json.loads( new_string )

Or escape your \ characters:

import json

new_string = """
{
        "styles":[
                {
                        "label":"Style",
                        "target":{
                                "label":"Target",
                                "objectName":"*"
                        },
                        "overrides":{
                                "materialProperties":{
                                        "perObj":{
                                                "script":{
                                                        "code":"cvex myFn(string myObj=\\"\\"; export string perObj=\\"\\") { perObj = myObj; }  ",
                                                        "bindings":{
                                                                "myObj":"myObj"
                                                        }
                                                }
                                        }
                                }
                        }
                }
        ]
}
"""

json.loads( new_string )
Sign up to request clarification or add additional context in comments.

1 Comment

Hah! Success with the escaping escapes! Strangely, the raw string option didn't work. What I mean by didn't work was that if I wrote anything after the last " " ", the text was considered a multi-line comment.
0

I would recommend reading from an actual JSON file rather than embedding it into your Python code:

with open('path/to/file.json') as f:
    json_string = f.read()

Or, if you need the JSON parsed into Python objects (dicts, lists etc.):

import json

with open('path/to/file.json') as f:
    json_data = json.load(f)

1 Comment

This didn't succeed for whatever reason, but thanks for the suggestion. Houdini seems to be picky about the contents of the json file and its python interpretation of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.