简体   繁体   中英

Convert json to formatted string

I want to convert this json:

{
        "rate_limit_by": 
            [{   "type": "IP", 
                "extract_from_header": "X-Forwarded-For"
            }]
    }

to this:

"{\"rate_limit_by\": [{\"type\": \"IP\", \"extract_from_header\": \"X-Forwarded-For\"}]}".

So that i can send it as part of payload in request in Python .

And i have tried multiple methods for the same. json.dumps doesnt work cause it doesnt escape characters in this case &.replace(""",r"\"") doesnt work cause it creates the string like this:

{\\"rate_limit_by\\": [{\\"type\\": \\"IP\\", \\"extract_from_header\\": \\"X-Forwarded-For\\"}]}

( Below is the example of curl but i want to send the data in specific format using python request. ) My upstream expects data in certain format, as of now am sending data to upstream as below:

curl -i --request POST --data "rule_name=only_ip" \
--data-binary "@data.txt" \
--url http://localhost:8001/plugin/rules

Where data.txt looks like this:

rule={
        "rate_limit_by": [
            { "type":"IP", "extract_from_header": "X-Forwarded-For" }
        ]
    }

Am trying to convert it to:

curl -i --request POST -H 'Content-Type: application/json' --data-binary @data.json  http://localhost:8001/plugin/rules

Where data.json should like this

   {
        "rule_name" : "test_ip",
        "rule":"{\"rate_limit_by\": [{\"type\": \"IP\", \"extract_from_header\": \"X-Forwarded-For\"}]}"
    }

Now the value of "rule" is string with character escape. This am trying to achieve & am doing post using python. And below is the code for same:-

import requests
import json
import re

url = 'http://localhost:8001/plugin/rules'
rule = {
        "rate_limit_by": 
            [{   "type": "IP", 
                "extract_from_header": "X-Forwarded-For"
            }]
    }

rule = json.dumps(json.dumps(rule))

print(rule) #this output the data in correct format

obj = {
        "rule_name" : "test_ip",
        "rule": rule #but when used it here its get wrapped in two \\
    }
headers = {'Content-Type': 'application/json', 'Accept': 'text/plain'}

print(obj) 

r = requests.post(url, data=obj, headers=headers)

print(r.text)

Do you mean you want to access the items inside somehow?

You should drop the "[]" because that part doesn't really make sense.

import json
x = str({
        "rate_limit_by": 
            [{   "type": "IP", 
                "extract_from_header": "X-Forwarded-For"
            }]
    })

x = x.replace("[","")
x = x.replace("]","")
x = eval(x)
d = json.dumps(x)
l = json.loads(d)
l['rate_limit_by']['type']

This outputs "IP". Now you have a dictionary of all you need called l.

desired is what you say you need in your something.json file. The following prints True . See https://repl.it/repls/DistantTeemingProtocol .

import json

desired = r'''{
        "rule_name" : "test_ip",
        "rule":"{\"rate_limit_by\": [{\"type\": \"IP\", \"extract_from_header\": \"X-Forwarded-For\"}]}"
    }'''

d = {
    "rate_limit_by": [{
        "type": "IP",
        "extract_from_header": "X-Forwarded-For"
    }]
}

s = json.dumps(d)
xxx = json.dumps({"rule_name": "test_ip", "rule": s}, indent=4)
o = json.loads(desired)
yyy = json.dumps(o, indent=4)

print(xxx == yyy)

If you're going to POST using requests, then you should not be posting the string but should instead be posting the dictionary.

Ie,

r = requests.post(url, json={"rule_name": "test_ip", "rule": s})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM