PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] How to URL encode data in JSON format in Python?

 July 23, 2022     encode, json, python, urlencode     No comments   

Issue

The code:

import urllib.parse

print(urllib.parse.urlencode({"59764":1}))

The problem: The print function should return %7B%2259764%22%3A1%7D but instead returns 59764=1

Any help is appreciated. Thanks!


Solution

You're urlencoding a dictionary.

Instead, you should json.dumps the dictionary to a string then quote it:

import json
import urllib.parse

value_dict = {"59764": 1}
value_str = json.dumps(value_dict)
print(urllib.parse.quote(value_str))

Output: %7B%2259764%22%3A%201%7D



Answered By - Michael Lindemuth
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing