JSON Parsing issue - Python - python

So i have build a REST Client that returns JSON response. However, i have an issue, where the JSON output is not exactly what i need:
Current Response:
{
"output": {
"status": "Device 'Test' does not exist",
"result": "null",
"response": {
"output": "success",
"result": 204
}
}
}
This output has an outermost enclosing "output" key, but i don't want that to be present. So basically i want my response to look like below:
{
"status": "Device 'Test' does not exist",
"result": "null",
"response": {
"output": "success",
"result": 204
}
}
I did try converting the JSON to Dict and then remove it, but no luck? any suggestions how to achieve this?
Thank you

if your response is already a dictionary or a json object then you can do following
value_required = response["output"]
if it is in text format (which I think it is) then you just need to do the following
import json
value_required = json.loads(response)["output"]

You should be able to do:
response = json.loads(response)['output']

Related

How to call JDoodle API from Python/Flask app

I'm a student, and I'm looking for help calling Jdoodle's compiler API (docs here), please.
When I make a post request through Postman and use this as the JSON body, it works...
{
"script": "console.log('hello world')",
"stdin": "",
"language": "nodejs",
"versionIndex": "0",
"clientId": "a3462eccc82ecc57a745a23e52c5c71e",
"clientSecret": "another long string"
}
...and I get the output I expect:
{
"output": "hello world\n",
"statusCode": 200,
"memory": "22764",
"cpuTime": "0.05"
}
However, I can't get it to work from my Python Flask back-end. Here is my code:
#app_bp.route("/compile", methods=["POST"])
def compile():
path = "https://api.jdoodle.com/v1/execute"
query_params = {
"script": "console.log('hello world')",
"stdin": "",
"language": "nodejs",
"versionIndex": "0",
"clientId": "a3462eccc82ecc57a745a23e52c5c71e",
"clientSecret": "another long string here similar to one above"
}
response = requests.post(path, params=query_params)
return response.json()
I've also tried this by passing in headers (headers = {"Content-Type" : "application/json"}), and I still get the same response from Postman:
{
"error": "Invalid Request",
"statusCode": 400
}
I'm a newbie, and any help would be greatly appreciated. Thank you!

Elasticsearch: return specific fields from within json in .csv/table format

I am a new Elasticsearch user, but I am struggling to accomplish something that was easy for me in Splunk. There are a few specific fields that I want from each event in my search, but the search "hit" outputs are always returned in a big json structure that is 95% useless for me. I do my searches with the python requests module, so I can parse the results I want in python when they return, but I have to access millions of events and performance is important, so I hope there is a faster way.
Here is an example of one single event returned from an Elasticsearch search:
<Response [200]>
{
"hits": {
"hits": [
{
"sort": [
1559438581000
],
"_type": "_doc",
"_source": {
"datapoint": {
"updated_at": "2019-06-02T00:01:02Z",
"value": 102
},
"metadata": {
"id": "AB33",
"property_name": "some_property",
"oem_model": "some_model"
}
},
"_score": null,
"_index": "datapoint-2019.06",
"_id": "datapoint+4+314372003"
},
What I would prefer is for my search to return only results in a table/.csv/dataframe format of the updated_at,value,id,property_name,oem_model values like this:
2019-06-02T00:01:02Z,102,AB33,some_property,some_model
..... and similar for other events ...
Does anyone know if this is possible to do with Elasticsearch or with the requests library without parsing the json after the search output is returned? Thank you very much for any help.
Yes, sure with the source filtering. Doc here
You filter the field to be returned from your query, so in this way tou choose only the useful fields and then you should not parse the json. Have a look here:
from elasticsearch import Elasticsearch
es = Elasticsearch()
query = {
"_source": [ "obj1.*", "obj2.*" ], #this is the list of the fields that you would return as a doc
"query" : {
"term" : { "user" : "kimchy" }
}
}
res = es.search(index="your_index_name", body=query)

How to validate a json structure in Python3

I am using Python3.5 and Django for a web api.When I refer to input, I refer to a HTTP request parameters. I have a parameter where I am expecting a JSON data which I need to validate before processing further.
I have a base json structure that the input has to be in.
Example,
{
"error": "bool",
"data": [
{
"name": "string",
"age": "number"
},
{
"name": "string",
"age": "number"
},
...
]
}
The above JSON represents the structure that I want my input to be in. The keys are predefined, and the value represents the datatype of that key that I am expecting. I came across a Python library(jsonschema) that does this validation, but I can't find any documentation where it works with dynamic data. i.e. the objects inside the JSON array 'data' can be of any number, of course this is the most simple scenario I came up with for explaining the basic requirement. In cases like these, how can I validate my json?
The solution here didn't help because it's just checking if the json is proper or not based on the Django model. My json has no relation with Django model. Its a simple json structure. It still doesn't tell me how to validate dynamic object
JSON Schema is a specification for validating JSON; jsonschema is just a Python library that implements it. It certainly does allow you to specify that a key can contain any number of elements.
An example of a JSON Schema that validates your code might be:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"required": [
"error",
"data"
],
"properties": {
"error": {
"type": "boolean"
},
"data": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
}
}
}
}
}
}
See https://spacetelescope.github.io/understanding-json-schema/ for a good overview
Take a look into the documentation of Python's JSON API. I believe json.tool is what you're looking for, however there are a couple of other ways to validate JSON using that API.

What is wrong with my patch request to the Connectwise Rest API?

I've got a question about the Rest API for Connectwise. I've been doing get and post requests with no issue, but when I do a patch request I get a 400 response with 'field value is invalid' message regardless of what I try. I'm on 2016v1 and using the Rest API making calls from Python with the requests library.
The Rest API documentation says the following object is supposed to be passed in the body, but I haven't a clue what values are supposed to go with these keys:
{
op (string, optional),
path (string,optional),
value (string,optional)
}
I've tried dozens of calls including with the following bodies:
{'summary': 'updatedsummarytext'}
{'value': {'summary': 'updatedsummarytext'}}
{'op': {'summary': 'updatedsummarytext'}}
I have only gotten the following response so far:
<Response [400]>
{
"code": "InvalidObject",
"message": "operations object is invalid",
"errors": [
{
"code": "InvalidField",
"message": "The field value is invalid.",
"resource": "operations",
"field": "value"
}
]
}
Is their a specific value that connectwise is expecting for the op or value keys, or is there something I'm missing unique to Patch rest api calls?
The PATCH calls at a basic level use RFC6902.
Consider the following (simplified) Ticket document:
{
"summary": "Initial Ticket Summary",
"id": 1,
"company": {
"id": 5
},
"board": {
"id": 10
}
}
If you wish to update the summary field, your PATCH request JSON would look like this:
[
{"op": "replace", "path": "/summary", "value": "Updated Summary"}
]
Hope this helps.

Python parsing using JSON for use with seleium

I'm trying to write a python script that will read in a JSON file with the aim to show how many screens are available and be able to pull the value of the different json fields
JSON
{
"screen": [
{
"id": "1",
"user": "user1#example.com",
"password": "letmein",
"code": "123456"
},
{
"id": "2",
"user": "user2#example.com",
"password": "letmein",
"code": "123455"
},
{
"id": "3",
"user": "user3#example.com",
"password": "letmein",
"code": "223456"
}
]
}
Python
import json
from pprint import pprint
with open('screen.json') as data_file:
data = json.load(data_file)
#json_file.close()
pprint(data)
data["screen"][0]["id"]
As you can see from python script I can successfully print out the json file is pprint but when I try just find print out individual values I'm getting stuck
Am I doing something wrong here?
I want to be able to use all the values in json files as variables later on in python script to be able to be used with selenium to open a web page using this values?
I tested your example code and it works fine. It looks like you just forgot to actually print the value in the final line. That is:
data["screen"][0]["id"]
should be
pprint(data["screen"][0]["id"])
which prints u'1' when I try it.

Categories