I am deserializing strings using JSON.parse. Most of the time, I have a contentful object serialized into a string, and JSON.parse works on that string, but in some cases, I want to send a minimum input to JSON.parse, whose result will just be thrown away. When I send "" like: JSON.parse(""), it returns an error: unexpected token at '""'. What is the restriction of JSON specification that I am violating, and what alternative minimum string can I send to JSON.parse?
Reading the JSON specification that Musa provided a link to, one minimum object that can be stringified and sent to JSON seems to be:
[null]
or something like that. Or, in the stringified form, it will be:
"[null]"
I would send null as this represents "nothing".
require 'json'
nil.to_json
#=> "null"
Related
I'm storing some data in Redis and when I retrieve this data I'm having trouble parsing it.
When I run this:
$redis.get("data") I get this: "[{\"login\"=>\"name\", \"id\"=>1574}]"
When I try to use JSON.parse against the return body I get this error:
JSON::ParserError: 409: unexpected token at '{"login"=>"name", "id"=>1574}]'
What am I doing wrong?
You need to first serialize your ruby object into JSON before storing it in redis.
In this case you would need to call $redis.set('data', JSON.dump(data)) instead of what is currently being called, which is $redis.set('data', data.to_s). The .to_s gets called on whatever object is passed into redis' set method.
If you really need to parse the data you have already stored then you can use ruby's eval, although you should not use this in production!
$ data = eval($redis.get('data'))
=> [{"login"=>"name", "id"=>1574}]
$ $redis.set('data', JSON.dump(data))
"[{\"login\"=>\"name\", \"id\"=>1574}]"
is not json string
If you want to deserialize string then you write this.
"[{\"login\":\"name\", \"id\":1574}]"
=> is changed to :
So, you need to modify the creation JSON string.
I am using net library in go and I want to make RPC call:
Client.Call("action", []string{"arg1", "arg2"}, &response)
But in JSON I see:
{"method":"action","params":[["arg1","arg2"]],"id":0}
Notice that arguments are enclosed with double square brackets.
In my case I need params to be a simple list:
{"method":"action","params":["arg1","arg2"],"id":0}
Any ideas how to accomplish this?
The codec that Go's JSON RPC uses on top of the rpc.Client will take whatever param you send and encode that as the first element of the array it uses for the params.
So the encoded request will always have a top level array with just one element, which will contain the params you sent, as you already noted.
See the WriteRequest function here:
https://golang.org/src/net/rpc/jsonrpc/client.go#L57
To achieve what you want, you can implement a custom rpc.ClientCodec.
The interface is documented here:
https://golang.org/pkg/net/rpc/#ClientCodec
You can borrow almost all of the implementation for the default JSON codec here:
https://golang.org/src/net/rpc/jsonrpc/client.go
And modify the params attribute of the request to read:
Params interface{} `json:"params"`
Then when writing your WriteRequest based on the standard one, you can just assign your params to the request params:
c.req.Params[0] = param
You can then use the rpc.NewClientWithCodec to create a client using your custom codec:
https://golang.org/pkg/net/rpc/#NewClientWithCodec
I am using a SOAP API that returns XML but with JSON strings within the response envelope. For most of the API calls this has not been a problem but there is one that returns Javascript new Date objects that is causing problems when using JSON.parse. Here is a simplified example of the response I am getting.
"{\"History\":[ {\"Timestamp\":new Date(1380024020923)}]}"
When using JSON.parse I get the following error.
JSON::ParserError: 399: unexpected token at '{"Timestamp":new Date(1380024020923)}]}'
Is there a nice way to parse this string or am I going to have to use some regex/string trickery? Has anyone come across this way of returning a date object and I would like to understand the advantage?
HTTParty's parsed_response method returns a Hash if you get a response code 200 but otherwise it will return a String regardless if the webserver returns a XML response.
HTTParty.get(post_url).parsed_response.class # Depends on response code
Amazon will provide XML (explaining what went wrong) even on a 403.
Am I missing something?
HTTParty parses its #parsed_response based on what the HTTP response's Content-Type header is. Verify what the value is for that HTTP header. In your case, you'd want it to be application/xml.
In case anyone is still encountering this issue today, get can take a format parameter, which can help you ensure your HTTParty::Response object is a Hash:
url = HTTParty.get('http://domain.com', format: :json) # class is a Hash
I have serialized an object in YAML and send it to a remote worker.
The worker doesent have the object definition so i get a YAML::Object.
How can i access the field inside it?
A text field seems like that base64 encoded, how can i decode that? (no, decode64 not works).
you can pass the object as something "known between both sides" (like an openstruct or hash) or give the description to the client.
It would be interesting to have a serialization format that also serialized the class and its methods...I'll have to think about that one...
try c["bar"]
you can also see all the provided keys using c.keys