-1

I'm developing a function that gets a dictionary as string and I need to append to another one as dictionary. After that, I need to parse it like JSON to do a post request. My problem is that I can't parse properly and the final JSON don't have a nested dictionary. The function is like this:

def namefunction(field_string):

    dfield01 = "1234567890"
    dfield02 = "abcdefgh"
    headers = { 'content-type': 'application/json', 'Accept-Charset': 'UTF-8' }
    endpoint = "https://url.to.endpoint"

    dataSend= { "dfield01": dfield01, "dfield02": dfield02 }
    dataSend["field_string"] = json.loads(field_string)

    r = request.post(url=endpoint, data=json.dumps(dataSend), headers=headers)

    return HttpResponse(r, mimetype='aplication/javascript')

The string that I get in this function is like this:

field_string = '{"0":"whatever 000","1":"whatever 001\nwhatever001b","2":"whatever 002\nwhatever 002b"}'

I'm using Python2.7 (I can't change that).

#### 2019-09-17 Update

Thank for your suggestion @alex. I did a similar aproach using json.loads(), but when I try to put the output in another dictionary, the first one loses his format. I will try to explain with an example:

With this...

# the same -> field = json.loads(field_string)
field = ast.literal_eval(field_string)

return HttpResponse(field, mimetype='application/javascript')

I get a JSON format in the output, but if I do this ...

data = { "name_field": json.loads(field_string) }

... or this:

data = { "name_field": ast.literal_eval(field_string) }

When I check the data output, I get a string in the value. What I'm doing wrong?

####

Any clues, please?

Regards.

SLR
  • 1
  • 1
  • Check out https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary – alex Sep 16 '19 at 15:14
  • 1
    `field_string` is neither valid JSON nor the string representation of a dict. It's missing brackets. That's what you need to fix. – glibdud Sep 16 '19 at 15:15
  • Thank you @alex for your suggestion. I will update the answer with more info. – SLR Sep 17 '19 at 05:54
  • Your `field_string` is missing `{}` if you expect it to be evaluated as a dict. – Grismar Sep 17 '19 at 05:58
  • Hello @Grisma. Yes, you are right, without {} we can't get the dictionary. I mislead them in the example, but actually I have {} in the string. With the brackets the behaviour is the explained above this. I will updtae that in the example. – SLR Sep 17 '19 at 07:46
  • Per `data = { "name_field": ast.literal_eval(field_string) }` *When I check the data output, I get a string in the value* - is `data` really a string? You just assigned a dictionary to that variable. – alex Sep 17 '19 at 13:44
  • No, the field data is a dictionary, but the value of "name_field" shows up like a string when I append it. For example, the output of this: `data = { "field1": "value1, "field2": "value2", "name_field": ast.literal_eval(field_string) }` is: `field1: "value1" field2: "value2" name_field: "{"0":"whatever 000","1":"whatever 001\nwhatever001b","2":"whatever 002\nwhatever 002b"}"` when I expect this: `field1: "value1" field2: "value2" name_field: { "0":"whatever 000", "1":"whatever 001 whatever001b", "2":"whatever 002 whatever 002b" }` – SLR Sep 17 '19 at 13:59

1 Answers1

0

This is an excellent example of why providing example code that actually reproduces the problem would be a better way to go.

When using your code:

import json
field_string = '{"0":"whatever 000","1":"whatever 001\nwhatever001b","2":"whatever 002\nwhatever 002b"}'
data = { "name_field": json.loads(field_string) }

Generates an error, your code doesn't run. "json.decoder.JSONDecodeError: Invalid control character at: line 1 column 38 (char 37)"

After running, single letter change (field_string is a raw string):

import json
field_string = r'{"0":"whatever 000","1":"whatever 001\nwhatever001b","2":"whatever 002\nwhatever 002b"}'
data = { "name_field": json.loads(field_string) }

Appears to be fine?

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Hi @Grismar! Thank you, but the source for **field_string** is a html field, and I can't add an "r" before. – SLR Sep 19 '19 at 06:18
  • You don't need to, I added the 'r' to make sure you'd get exactly that from the script. Since that's what you posted in your example code, I'm assuming that's actually what you get. If not, you need to post code that will give people what you get, otherwise it's hard to help you. – Grismar Sep 20 '19 at 00:57
  • Hi @Grismar! Yes, the value's structure of the **field_string** is correct, and when I use the 'r' in a separate script with json.dumps(), I get the expected result (without 'r' I get the same error as you). – SLR Sep 20 '19 at 06:09
  • Sorry, but since you're not providing a complete example that actually reproduces the error, or makes clear exactly when the error occurs and how it differs from what is stated above, it's impossible to help you any further. – Grismar Sep 21 '19 at 11:18