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?

SMortezaSA
  • 589
  • 2
  • 15
Artur
  • 23
  • 2
  • 2
    What is different about the output from Python? – Retired Ninja Apr 18 '20 at 12:41
  • Something similar is being asked [here](https://stackoverflow.com/questions/42596710/can-i-deserialize-json-to-class-like-c-sharp-newtonsoft-in-python) and [here](https://stackoverflow.com/questions/15476983/deserialize-a-json-string-to-an-object-in-python) – tejuguasu Apr 18 '20 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 [mcve]. – dbc Apr 18 '20 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` – Nathan majicvr.com Apr 18 '20 at 14:35

2 Answers2

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.

tejuguasu
  • 11
  • 3
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)
Nathan majicvr.com
  • 950
  • 2
  • 11
  • 31
  • 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? – Artur Apr 18 '20 at 16:27
  • 1
    Actually, after inserting this part to code and adding the dump method as a file, I got the desired result. Thanks a lot! – Artur Apr 18 '20 at 16:52