Link to home
Start Free TrialLog in
Avatar of Steve Jennings
Steve Jennings

asked on

Best practice for posting JSON from python?

I am using python and REST API to post JSON data. My code reads a JSON file containing data from a Remedy change request  and collects values for variables in a 500-line JSON object that I post to another program. 


Also, it may be necessary to add multiple lines of JSON to be posted depending on the action in the change ticket.


I realize that 500 lines isn't very large, but it looks clunky in my python code and I wondered if there is a different or preferred way for posting JSON that needs to be "manipulated" before posting? Maybe do all of the manipulation to the JSON file in one python script, then do the POST of the ("ready") JSON file in a different python script?


Thanks,
Steve

Avatar of David Favor
David Favor
Flag of United States of America image

Unsure how exactly your code works.

State one of these...

1) I'm generating a JSON payload in a POST command to another Website.

2) I'm providing a service which receives a POST, form which I'll pull a JSON payload to decode for some type of work.

Tip: Either option will likely be much shorter than 500 lines of code.

Answering the above questions, then attaching your Python code will likely produce best comments.
Avatar of Steve Jennings
Steve Jennings

ASKER

I am asking about best practice to deal with large chunks of (in my case, 500 lines) JSON within a python program that uses the "request" method to POST information. It has nothing to do with the size (number of lines) of my code. I guess I wasn't clear, because you don't seem to understand what I am asking.

But to clarify:

The output of an Ansible playbook provides 10 to 200 key:value pairs in JSON format.
eg:
   "ChangeNumber":"CRQ1234",
   "DeviceName":"SomeSwitch",
   "Approved":"no",
   "Action":"Syntax Check"

and on and on . . .

My code reads that JSON output and plugs those 10 to 200 values into about 500 lines of JSON that I POST to another API endpoint.
eg:
   "ChangeNumber":var1,
   "DeviceName":var2,
   "Approved":var3,
   "Action":var4

Those are not the variable names, by the way. But hopefully you get the picture. And the actual JSON is far more complicated than I am listing here. And, if there are multiple devices, there's extra JSON that has to be added . . . it isn't as simple as what I've depicted above. And there are 22 different Actions that can be selected, all of which have blocks of JSON with different key:val pairs to be completed.

So  . . . I am not asking how to write the code. I am asking whether best practice is to have the JSON object that I POST as lines within the python code that does the request / POST or  . . . some other way.
So  . . . I am not asking how to write the code. I am asking whether best practice is to have the JSON object that I POST as lines within the python code that does the request / POST or  . . . some other way.
Python comes with a built-in package called json for encoding and decoding JSON data.  

Check this for more information:
https://realpython.com/python-json/
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial