When ı print my json_dump
{"accountNumber": 4176312, "currencyCode": "USD", "ownerName": "selman", "accountType": "individual", "balance": 0.0}
But when ı return
return make_response(jsonify(json_dump), status_code)
I see this
Why this ///// is coming ? How can ı solve this ?
json_dump is, presumably, a string of JSON, (representing an object).
jsonify will convert that string to a JSON representation of that string which will cause any " in it to be escaped.
Send json_dump to the browser (where you want a JSON representation of an object).
Don't use jsonify on something that is already JSON (since a JSON representation of a JSON representation of an object isn't very useful).
Related
I'm using Scrapy to extract some values from a website. One of the fields is "price" and I have something like:
...
item = {
"title": title,
"url" : url,
"price": {
"symbol": the_currency_symbol,
"amount": float_number
}
}
yield item
I set the output to be a JSON file and I yield a dictionary item.
The problem is that when I open the output JSON with the items I see this:
{
"title": title,
"url" : url,
"price": {
"symbol": "\u00a3",
"amount": 12.99
}
}
How can I see the correct currency symbol in the JSON file?
Scrapy normally produces JSON feeds in ASCII encoding. Your JSON file has correct data but to see the currency symbol properly you can convert the output JSON file to UTF-8 encoding.
You can make Scrapy generate JSON in UTF-8 encoding by setting FEED_EXPORT_ENCODING="utf-8". For more help, see the answers to the question: Scrapy json response convert in utf-8 encode and Scrapy documentation https://docs.scrapy.org/en/latest/topics/feed-exports.html#std-setting-FEED_EXPORT_ENCODING.
If you do not want to run the scraper again then you can use some tool like https://stedolan.github.io/jq/ on your JSON file like jq file.json > outfile.json. Then outfile.json will have proper symbols.
I have a sample JSON body that contains some strings. I want some of the strings to be converted into Mongo Extended JSON. For example, the JSON body passed in is the following:
{
"GuidBinary": "734cba69-4851-4869-8d0e-e870d6fb3065",
"DateTime": "12/12/2012",
"RegularString": "abcd"
}
And I want to convert this into
{
"GuidBinary": {
"$binary": {
"base64": "<payload>",
"subType": 0x03
}
},
"DateTime": {"$date": "<ISO-8601 Date/Time Format>"},
"RegularString": "abcd"
}
Is there a way to do this in Go, either through a package like mongo-go-driver or another method?
Yes, it's possible. It's part of the official mongo-go driver. Generating this extended JSON is published as the bson.MarshalExtJSON() function.
Example using it:
m := map[string]interface{}{
"GuidBinary": []byte{1, 2, 3, 4, 5},
"DateTime": time.Now(),
"RegularString": "abcd",
}
out, err := bson.MarshalExtJSON(m, false, false)
fmt.Println(string(out), err)
This will output (try it on the Go Playground):
{"DateTime":{"$date":"2009-11-10T23:00:00Z"},"RegularString":"abcd",
"GuidBinary":{"$binary":{"base64":"AQIDBAU=","subType":"00"}}} <nil>
So what you need to do is unmarshal your original JSON (using the encoding/json package), and you need to do some post processing on it: GuidBinary is a regular string in the input JSON, but it represents a UUID. You need to parse it into a UUID value (there are probably lots of libraries for this).
You also need to convert (parse) the DateTime which is also given as a JSON text in the input, but it represents a date. Use time.Parse() for this. And now you can call bson.MarshalExtJSON() to generate the extended JSON.
I have json input like this
{
"receivedNumber": "\357425078",
"receivedRma": "RL975F0718331133212",
"EndDate": "12/16/2017 12:54:13 PM",
"manufacturerDate": "11/14/2016",
"firstUseDate": "12/17/2016 11:58:20 AM"
}
for this input, 'json to object transformer' getting failed because of escape sequence in 'receivedNumber'
is there any solution regarding this ? how to handle escape sequence ?
try this
"{ \"receivedNumber\": \"\357425078\", \"receivedRma\": \"RL975F0718331133212\", \"EndDate\": \"12/16/2017 12:54:13 PM\", \"manufacturerDate\": \"11/14/2016\", \"firstUseDate\": \"12/17/2016 11:58:20 AM\" }";
1 :
"{\"currentCity\":\"\U5317\U4eac\U5e02\",\"latitude\":\"39.784277\", \"userId\":\"81d911a3-9731-4153-85bc-b095614f020e\",\"longitude\":\"116.518057\",\"operationType\":\"2\"}"
2 :
{"currentCity":"Beijing","latitude":"39.784277","longitude":"116.518057","userId":"81d911a3-9731-4153-85bc-b095614f020e","operationType":"2"}
Question: Which is a json string ?
The first one. The second one is a string representation of a Javascript object. When you parse the first one, the second one is what you get (a Javascript object).
First one is the string representation of JSON object with escaping character to escape double quotes , while second one is java script object already , if you prettify second you see proper representation like this
{
"currentCity": "Beijing",
"latitude": "39.784277",
"longitude": "116.518057",
"userId": "81d911a3-9731-4153-85bc-b095614f020e",
"operationType": "2"
}
You can check and play with your JSON here: https://codebeautify.org/jsonviewer
I'm using JSON Path PostProcessor with path expressions to store a JSON object from a JSON response, but when I later retrieve the variable, it has been reduced to string with key - pair values. So I don't know that was a string or number.
Example:
Response looks like this
{
.
.
"currency" : {
"code" : "AUD",
"name" : "Australian Dollars",
"symbol" : "$"
},
.
}
Using the path expression, I find currency and save it.
However, when I use it in a HTTP Request body data ("currency" : ${currency},),
it comes like this:
"currency" : {code=AUD, name=Australian Dollars, symbol=$},
How do I get the JSON Path PostProcessor to save the JSON object 'as is" without losing the data type details? Should I be using a different approach instead of JSON Path?
I would recommend switching to JSR223 PostProcessor and Groovy language, this way you will have full control of what is going on and be able to do what you want.
Assuming you have the following JSON response:
{
"currency": {
"code": "AUD",
"name": "Australian Dollars",
"symbol": "$"
}
}
the relevant Groovy coode will be something like:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def currency = json.currency
def currencyString = new groovy.json.JsonBuilder(currency).toPrettyString()
vars.put('currency', currencyString)
log.info(currencyString)
Demo:
References:
Groovy - Learn
Groovy - Parsing and producing JSON
Groovy is the New Black