retrieve json from google app engine datastore - json

I have stored some simple data in the GAE datastore. Now I'd like to pull it out, but I'd like to do so with the results as JSON. Is there a simple way to do this?

You can first convert your datastore model to a dictionary and then use simplejson (python 2.5) or json (python 2.7) package to convert the dictionary to json. Typically this is how the last line of your handler will look like:
self.response.out.write(simplejson.dumps(some_datastore_entity.to_dict()))
The new ndb interface to the datastore provides a to_dict method by default. You can check it out here

class Handler(webapp2.RequestHandler):
def get(self):
<do your GQLQuery here>
self.response.headers['Content-Type'] = 'application/json'
self.response.body = json.dumps(<your data in dict or dict list>)
self.response.set_status(200)

Related

How to convert request data to json in AWS Lambda services?

"error%5Bcode%5D=BAD_REQUEST_ERROR&error%5Bdescription%5D=Payment+failed&error%5Bsource%5D=gateway&error%5Bstep%5D=payment_authorization&error%5Breason%5D=payment_failed&error%5Bmetadata%5D=%7B%22payment_id%22%3A%22pay_Es97gMGzx61l1u%22%2C%22order_id%22%3A%22order_Es96Rxp5OmnVVF%22%7D"
We are currently migrating to Lambda services from Flask. In Flask I was able to get the data in dictionary but in the AWS Lambda services, I am receiving the data as string, does anyone know how to parse this or convert it into a json or dictionary?
Thanks for ur time (:
This example string looks like an encode URI String. Where do you get it ? Could you provide more informations about usage context : API Gateway or request from another lambda, and how do you get this this?
With Pyhton usually you can get the path parameters and the query string parameters as a dictionnary with :
def my_handler(event, context):
params = event["pathParameters"]
query = event["queryStringParameters"]

Spark Read JSON with Request Parameters

I'm trying to read a JSON response from IBM Cloud's DB2 Warehouse documentation. This requires me to pass a request body wherein I have to supply userid and password as request parameters.
To read using spark.read.json, I did not find anything wherein request parameters could be supplied. Is there anyway using which we could do that?
Usually I would read the JSON using Scala alone using scalaj-http and play-json libraries like:
val body = Json.obj(Constants.KEY_USERID -> userid, Constants.KEY_PASSWORD -> password)
val response = Json.parse(Http(url + Constants.KEY_ENDPOINT_AUTH_TOKENS)
.header(Constants.KEY_CONTENT_TYPE , "application/json")
.header(Constants.KEY_ACCEPT , "application/json")
.postData(body.toString())
.asString.body)
My requirement is I cannot use these 2 libraries and have to do it using scala with the spark framework.
You can not use spark.read.json directly for REST API data ingestion.
First, make your API call request to get response data and then convert it to DataFrame with Spark. Note that if your API is paginated then, you'll need to make multiple calls to get all data.
For your example, you need to call authentication endpoint in order to get a Bearer token and then add it to the request header :
Authorization: Bearer <your_token>
All this part could be done using only Scala (example scala.io.Source.fromURL).
Once you get the response_data, use spark to convert it to DF :
import spark.implicits._
val df = spark.read.json(Seq(response_data).toDS)

What's the proper way to extract data from a google protobuff object?

I have never encountered this sort of collection or object before until now (its the response from a request to Google-Cloud-Vision API).
I wrote a class that uses the API and does what I want correctly. However the only way that I can extract/manipulate data in the response is by using this module:
from google.protobuf.json_format import MessageToJson
I basically serialized the protobuff into a string and then used regex to get the data that I want.
There MUST be a better way than this. Any suggestions? I was hoping to have the API response give me a json dict or json dict of dicts etc... All I could come up with was turning the response into a string though.
Here is the file from the github repository:
image_analyzer.py
Thank you all in advance.
The built in json module will parse the string into a dictionary, like json.loads(MessageToJson(response1)).
You can just access the fields in the message object directly, e.g.:
response1 = vision_client.face_detection(image=image)
print(response1)
print(response1.face_annotations[0].detection_confidence)

Google Cloud Endpoints allowed method's return types

I'm developing a REST API using Cloud Endpoints. I know that as per the documentation each API method should return an entity that is then automatically converted to a valid JSON string.
However, I'm dealing with an authentication library that in some cases returns a JSON which should be passed back to the client as a response.
Sticking with the default approach, meaning returning an entity, would still be possible, but it would involve a number of obnoxious intermediate steps, like parsing the JSON and filling the right fields of the entity to be returned according to the JSON content.
I was wondering if there is a more straightforward way to instruct the API to directly return the JSON string, instead of converting it to an entity just to have it translated back to the source JSON.
One thing that probably may help you.
Suppose you have a RPC response class as
from protorpc import messages
from protorpc import message_types
from protorpc import remote
class ApiResponse(message.Messages):
response = messages.StringField(1, required = True)
You can return your JSON in response field by using JSON module. First of all import json in your endpoints api
import json
Suppose your response is
json_response = [{'name': 'Jack', 'age':12}, {'name': 'Joe', 'age':13}]
In your response to this api you can do this:-
return ApiResponse(response = json.dumps({'data': json_response}))
json.dumps() converts your dictionary object into JSON string that can be passed to response of ApiResponse class.
After you receive response in client side(javascript), you can simply parse it to JSON using
JSON.parse(response)

DRF testing: instead of JSON an OrderedDict is returned

I'm trying to implement tests for the Django Rest Framework.
Most of my tests pass and setting them up went smooth, but i'm having an issue now where an assertEqual never succeeds because it keeps comparing JSON with an OrderedDict.
I have no idea where the OrderedDict comes from since DRF should only return JSON (right?).
Might it be possible that the testing environment is parsing the JSON before comparison? That would suck.
I'm doing an integrated test that only tests the data in the response of a GET request to a certain resource, I do this based on JSON fixtures. I'm not testing a specific component of the REST framework since my implementations of the components are so simple they're already tested by the tests in the DRF project.
Anyways, I hope someone can help me!
As explained here, this is because the default format for requests during tests is multipart instead of json. You can specify the format by providing it to your api call like so:
response = self.client.get('/something/1', format='json')
Or you can set the default test request format in your settings.py like so:
REST_FRAMEWORK = {
'TEST_REQUEST_DEFAULT_FORMAT': 'json', # Use application/json instead of multipart/form-data requests in tests.
}
To fix it for all your tests automagically.
It sounds like you're using response.data (which returns the parsed json objects) instead of response.content (which gives the raw json string).
See http://www.django-rest-framework.org/api-guide/testing/#testing-responses
If your tests look something like this:
class SomeTests(APITestCase):
def test_something(self):
response = self.client.get('/something/1')
# assertions with response
Then response will certainly be an OrderedDict rather than a JSON document. Luckily Django 1.9 has introduced the response.json() method (https://docs.djangoproject.com/en/1.9/topics/testing/tools/#django.test.Response.json) so you can easily convert the response into JSON. Note that you could also use python's json library.
The catch here is that Django's test client (that DRF extends) is a "dummy browser" (https://docs.djangoproject.com/en/1.9/topics/testing/tools/#the-test-client) and doesn't work exactly like an in-browser framework such as Selenium would. Thus, HTTP calls are actually just simulated HTTP calls that focus on testing your logic and that correct routing/views/serializers/etc. are being used.
You can dump your data into json format :
import json
return HttpResponse(json.dumps(data))
I solved the problem by using SerializerMethodField.
Simply, within the serializer class copy next last 3 lines and replace result with the json member that cause the problem
class ConfigSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Config
fields = ('id', 'url', 'email', "result",)
# COPY NEXT 3 LINES AND CHANGE 'result' WITH THE JSON MEMBER THAT CAUSE THE PROBLEM
result = serializers.SerializerMethodField()
def get_result(self, obj):
return obj.result
Before the result shows as:
{result: "OrderedDict([('key1', 1), ('key2', OrderedDict([('key3', [1, 2, 3])]))])"}
After solution, result become:
{"result": {"key1":1,"key2":{"key3":[1,2,3]}}}