Python Forum
Trying to get JSON object in python and process it further
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to get JSON object in python and process it further
#1
Hi,

Am trying to extract a specific field in JSON in python and process it further to send output in e-mail. The problem am facing am able to get the messageId, but nothing else works. Am trying to get userid and phoneno value into variable and use it for further processing, but the output is empty.

But if I try to get messageID, it works, the output is value of messageId
us = data_dict.get('messageId')
print us

Any other key I try to extract doesn't work, any help would be great!

#Display the dictionary in newline
    for key, value in data_dict.items():
       key = key.strip('/').strip('\n')
       value = value.strip('/').strip('\n')
       print(f"KEY:{key}: VALUE:{value}<br>")
   
    us = data_dict.get('userid')
	number = data_dict.get('phoneno')
JSON input looks as below:

Output:
messageId: zZCqqqXvoKx567894QM1 conversationId: XmlgyWS48Qrthuwehs data: {"action":"submit","userid":"xyxr","phoneno":"567894567"},"formMessageId":"LDFWN_ha-fdfdffdeasqwcsd","formId":"user-details-form"}
Reply
#2
That looks like improperly formatted json data.
The second dictionary doesn't contain an opening brace.
buran likes this post
Reply
#3
I guess this is the output from the print and the actual JSON is most likely

Output:
{ "messageId": "zZCqqqXvoKx567894QM1", "conversationId": "XmlgyWS48Qrthuwehs", "data": { "action": "submit", "userid": "xyxr", "phoneno": "567894567" }, "formMessageId": "LDFWN_ha-fdfdffdeasqwcsd", "formId": "user-details-form" }
import json
spam='''{
    "messageId": "zZCqqqXvoKx567894QM1",
    "conversationId": "XmlgyWS48Qrthuwehs",
    "data": {
        "action": "submit",
        "userid": "xyxr",
        "phoneno": "567894567"
    },
    "formMessageId": "LDFWN_ha-fdfdffdeasqwcsd",
    "formId": "user-details-form"
}'''

json_data = json.loads(spam)
print(f"userid: {json_data['data']['userid']}")
print(f"phoneno: {json_data['data']['phoneno']}")
Output:
userid: xyxr phoneno: 567894567
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question about Creating an Automated Process in Python Supratik1234 0 1,168 Jan-13-2023, 08:29 PM
Last Post: Supratik1234
  Python Split json into separate json based on node value CzarR 1 9,518 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  python update binary object (override delivered Object properties) pierre38 4 2,787 May-19-2022, 07:52 AM
Last Post: pierre38
  Deserialize Complex Json to object using Marshmallow tlopezdh 2 2,911 Dec-09-2021, 06:44 PM
Last Post: tlopezdh
  Process the image on the Python HTTP server Aleks 0 4,017 Dec-02-2021, 11:43 PM
Last Post: Aleks
  finding and deleting json object GrahamL 1 5,867 Dec-10-2020, 04:11 PM
Last Post: bowlofred
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 2,484 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer
  Spawning a new process that is not attached to python cman234 3 3,008 Apr-25-2020, 05:24 PM
Last Post: cman234
  difficulties to chage json data structure using json module in python Sibdar 1 2,683 Apr-03-2020, 06:47 PM
Last Post: micseydel
  How to process JSON response from requests? Heinrich 5 5,360 Jan-10-2020, 06:17 PM
Last Post: Heinrich

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020