2

I am trying to do something similar in Python like NewtonSoft is doing in C#.

I have the following JSON file:

{
    "lead": {
        "Id": "abc",
        "CreateDate": "2020-16-04T17:55:47.229554",
        "Source": "Source"
    },
    "cars": [
        {
            "Id": 1,
            "Year": "1951",
            "Make": "Willys"
        },
        {
            "Id": 2,
            "Year": "1950"
        }
    ],
    "Client": {
        "LeadId": "ca5326c1fa14475ea6e8106c8c4a3d9d",
        "FirstName": "Christopher",
        "LastName": "Murphy"
    }
}

If I serialize the JSON and use NewtonSoft in C# then I get the following result:

"{\"lead\":{\"Id\":\"abc\",\"CreateDate\":\"2020-16-04T17:55:47.229554\",\"Source\":\"Source\"},\"cars\":[{\"Id\":1,\"Year\":\"1951\",\"Make\":\"Willys\"},{\"Id\":2,\"Year\":\"1950\"}],\"Client\":{\"LeadId\":\"ca5326c1fa14475ea6e8106c8c4a3d9d\",\"FirstName\":\"Christopher\",\"LastName\":\"Murphy\"}}"

But the dump and dumps methods are giving other results in Python, Is there other way to achieve the same result in python?

4
  • 2
    What is different about the output from Python? Commented Apr 18, 2020 at 12:41
  • Something similar is being asked here and here Commented Apr 18, 2020 at 12:47
  • 1
    But the dump and dumps methods are giving other results in Python - then please edit your question to include the code that doesn't work -- i.e. a minimal reproducible example. Commented Apr 18, 2020 at 13:22
  • Hello, welcome to SO! Why do you need it in that format? If you 100% want it exactly like that, you could write 1-20 lines of code that make it that way; something like with open(json_fname, 'r') as f: json_lines=f.readlines(); out=''; for line in lines: out+=line Commented Apr 18, 2020 at 14:35

2 Answers 2

1

As long as there are no weird edge cases I'm unaware of, you could do something like this

def serialize(json_fname):
  with open(json_fname, 'r') as f:
    json_lines=f.readlines()

  out=''
  for line in json_lines:
    out+=line
  out=out.replace(' ','')
  return out.replace('\n','')

serialized=serialize('myjson.json')

#  Later in the program:
#    do(serialized)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot this helped, but what I meant is when in C# you use NewtonSoft, then it automatically inserts this backslashes "\". E.g "{\"lead\":{\"Id\":\"abc\"..., do you know is there anything similar in Python?
Actually, after inserting this part to code and adding the dump method as a file, I got the desired result. Thanks a lot!
1

Sorry, misunderstood the question. What you need is to escape the serialized string with replace or other method.

Also, there may be some differences between the object's real value, and the value you see when using debug tools or depending on the IDE.

Comments

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.