Sending string in python - json

I use the JSON in python version 3.4 for the network programming. The server program was built with another language and it expects to recieve the message in string. I put the JSON query into string and when send it with sock.send(query.encode()), but I get the message incorrect data type. Is there exist a way to send the string without converting it?

Related

Unescape JSON from terraform provider

I am developing a custom terraform provider in Go.
I have a JSON parameter like this:
local_context_data = jsonencode({"hello"="world"})
When I check what is sent to the API with TCPdump I can see that the JSON is escaping like this:
{\"hello\":\"world\"}.
But it generates an error because the API is waiting for real JSON without escape.
Do we have a function in Go to unescape this?
Or better: do we have a function in the terraform SDK?
if your API expects json, what you want is probably
local_context_data = {"hello"="world"}
You only need jsonencode if you want to send a json string within the json.

Sonar Issues/search API JSON result has single quotes

I'm using the sonarQube 6.4 web api to get a list of issues
http://sonar-server:9000/api/issues/search?componentKeys=Project_key&sinceLeakPeriod=true&statuses=OPEN,REOPENED&types=BUG
This gives me a Json object which has single quotes,
..."message":"Make this function anonymous by removing its name:
'function() {...}'."...
Because of that highlighted content in the JSON I'm unable to process the JSON from Groovy.
Is the JSON returned by the sonar is valid ?
if so, is there any way to process this kind of JSON in groovy.
Let me know if the full JSON object is needed.
According to http://json.org/ and https://jsonformatter.curiousconcept.com/ the JSON response is valid. Single quotes and brackets {} must not be escaped. The issue comes from your Groovy parser.

troubleshooting JAXBUnmarshalException: Unexpected end of stream (RESTeasy)

I would appreciate if someone could help me formulate a strategy for fixing the following error, which occurs in no discernible pattern; most of the time there is no error, and I am successful about 90% of the time:
ERROR [org.jboss.resteasy.core.SynchronousDispatcher] Failed executing POST /toys/customer25/insert
org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: Unexpected end of stream
How would I log this error simply to the console? I'd like to capture the JSON that this method is receiving and figure out what is causing this error. I would be able to see it in the server logs.
What I am doing is POSTing an array of Toy objects to the server, in JSON format. That is, on the client I transform my Toy object into a Toy JSON. The string that I am sending is like this: [{"toyName":"buzzlightyear", "toyMaker":"mattel"}]
My understanding is that when I POST JSON to a jax-rs restful service, a jax-rs implementation (I'm using RESTeasy 1.2) will somehow automagically convert that JSON string into a java Toy object.
My code on the Java JAX-RS restful service for handling a POST which inserts a Toy object is this:
#POST
#Consumes({MediaType.APPLICATION_JSON})
#Produces({MediaType.TEXT_PLAIN})
#Path("toy/{customer}/insert")
public String insertToys(#PathParam("customer") String customer, List<Toy> toys);
How do I log/capture what is wrong with the JSON string? It seems to me that the system is handling the conversion of the JSON to Toy object right when the insertToys() method is called. So it's hard form to figure out how to debug.
Can someone formulate a strategy for me? What is the likelihood that I am passing in malformed JSON, maybe from some unescaped character? Vs mapping entities wrong? Thanks!
Your JSON is incorrect.
The method accepts List, so you must send an array of Toy Objects:
[{"toyName":"buzzlightyear", "toyMaker":"mattel"}]
(note the braces).
For more objects:
[{"toyName":"buzzlightyear", "toyMaker":"mattel"},{"toyName":"rex", "toyMaker":"mattel"}]

How to send JSON data in an Ajax request?

what would be the best way to send string (unicode text) & array-of-string data (several hundreds characters, for now formatted as a single Json object) from an HTML page to a PHP server document (to be then recorded in a db)? I'm pretty much new at Json and I'm not sure how to send a Json object server side with an Ajax call.
Right now, my idea would be to somehow serialize my Json object into a string, then send that string as a parameter to a POST Ajax call, but again, I'm not sure if this is the right way or how to serialize Json.
I don't use jquery at this time.
You can use JSON.stringify as you suggested.
JSON.stringify
But why do you want to serialize as JSON on the client side as opposed to sending as is to the server and converting to JSON on the server side? Then you could use a JSON library in whatever language your server supports.

Get byte[] from MySQL in Json format

I've been trying for a while to get a byte[] from my database blob and then parse it to json and send it to my clients. I first get the blob from the database and then I get the byte array by doing:
MyObject temp = new MyObject()
Blob icon = dbResult.getBlob(1);
temp.setIcon(icon.getBytes(1, (int)icon.length()));
Later on I parse MyObject to a json string which I send back to clients. But it seems that the json string becomes pretty much corrupted when parsing the byte[].
An example of how it might look:
[{"icon":"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAFQAPwDASIAAhEBAxEB/8QAHAAAAQUBAQEAAAAAAAAAAAAAAgEDBAUGAAcI
I parsed this to json in my webservice which is a jersery webservice. When i try to parse it from json into a class object at client side, it throws IllegalStateException(Gson).
Anyone knows what this is about and what i'm doing wrong?
If you try to transfer binary data using json, you better explicitly encode it with base64 and then decode it on receiver's side. JSON isn't designed to wrap binaries, so the problems when trying to make this were expectable.