I am trying to parse JSON data in my Rails 5 application with JSON.parse. I keep getting this error:
"#<JSON::ParserError: 785: unexpected token at ': Hours,\n \"config\": {\n \"blocks\": <\n {\n \"type\": table,\n \"name\": Hours from Users,\n \"fields\": [\n \"customer\",\n \"project\",\n \"user\",\n \"task\",\n \"hour_type\",\n \"amount\"\n ]\n }\n >\n }>"
here is what it looks like if you print the message:
#<JSON::ParserError: 785: unexpected token at ': Hours,
"config": {
"blocks": <
{
"type": table,
"name": Hours from Users,
"fields": [
"customer",
"project",
"user",
"task",
"hour_type",
"amount"
]
}
>
}>
you forgot to quote certain values like table and Hours from Users, they need double-quotes " to be valid json.
(also not sure about the extra < and >, might be coming from the parser error)
Change from :json to :hybrid like:
Rails.application.config.action_dispatch.cookies_serializer = :hybrid
as explained in this Rails issue:
Related
I have a dict in json (ip_address) which consists of the following structure:
"0": {
"json": {
"code": 200,
"success": true,
"data": "10.100.146.3",
"time": 0.004
}
"1": {
"json": {
"code": 200,
"success": true,
"data": "10.100.146.4",
"time": 0.005
}
I'm trying to call the json twice, in seperate tasks like this:
"{{ ip_address + '.0.json.data' }}"
"{{ ip_address + '.1.json.data' }}"
This gives me the following error:
"Unexpected templating type error occurred on ({{ ip_address + '.0.json.data' }}): unsupported operand type(s) for +: 'dict' and 'str'"
I've tried various suggestions, here and on other guides/documentation but almost everything seems to give the same error. Anyone has any idea on how to solve this?
I am new in typesense,
I create one schema for the geoloc in typesense.
Schema
curl "http://localhost:8108/collections" \
-X POST \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: xyz" \
-d '{
"name": "demo",
"fields": [
{"name": "name", "type": "string" },
{"name": "city", "type": "string" },
{"name": "country", "type": "string" },
{"name": "iata_code", "type": "string" },
{"name": "_geoloc", "type": "string[]" , "facet": true },
{"name": "links_count", "type": "int32" },
{"name": "objectID", "type": "string" }
]
}'
I try to store with
curl "http://localhost:8108/collections/airports4/documents/import" \
-X POST \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: xyz" \
-d '
{"name": "Kalemie","city": "Kalemie","country": "Congo (Kinshasa)","iata_code": "FMI","_geoloc": { "lat" : -5.875556 , "long" : 29.25 },"links_count":1,"objectID": "1040"}'
But it returns an error
{"code":400,"document":"{","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 2: syntax error while parsing object key - unexpected end of input; expected string literal","success":false}
{"code":400,"document":"\t\"name\": \"Kalemie\",","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 8: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"city\": \"Kalemie\",","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 8: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"country\": \""Congo (Kinshasa)\",","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 11: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"iata_code\": \"FMI\",","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"_geoloc\" : [{\"lat\":-5.875556 , \"lng\": 29.25 }],","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 12: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"links_count\": 1,","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 15: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"\t\"objectID\": \"1040\"","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 12: syntax error while parsing value - unexpected ':'; expected end of input","success":false}
{"code":400,"document":"}","error":"Bad JSON: [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected '}'; expected '[', '{', or a literal","success":false}
I don't have any idea about how to store _geoloc with lat long.
You've to use a field of type geopoint.
Here's an example from the docs: https://typesense.org/docs/0.21.0/api/documents.html#geosearch
I have json format Splunk search results like below :
"{
"Name": "RUNQDATA",
"RunId": "2021021701",
"Details": <{
"RunQID": "796562",
"TQID": "796562",
"Ent": {
"NAME": "Inv",
"Store": {
"NAME": "FSW",
"TYPE": "QUEUE",
"USERNAME": "abc"
}
},
"ADD_COUNT": "5740",
"UPDATE_COUNT": "0",
"DELETE_COUNT": "0"
}>,
"status": "success",
}"
How can I extract the fields like ADD_COUNT or UPDATE_COUNT from this ? I tried spath & other options , however not able to get the required results. Probably because the json contains <>.
Any help here is appreciated.
Confirmed. If the angle brackets are removed then the spath command will parse the whole thing. The spath command doesn't handle malformed JSON.
If you can't change the format of the event then you'll have to use the rex command to extract the fields as in this run-anywhere example
| makeresults
| eval _raw="{
\"Name\": \"RUNQDATA\",
\"RunId\": \"2021021701\",
\"Details\": <{
\"RunQID\": \"796562\",
\"TQID\": \"796562\",
\"Ent\": {
\"NAME\": \"Inv\",
\"Store\": {
\"NAME\": \"FSW\",
\"TYPE\": \"QUEUE\",
\"USERNAME\": \"abc\"
}
},
\"ADD_COUNT\": \"5740\",
\"UPDATE_COUNT\": \"0\",
\"DELETE_COUNT\": \"0\"
}>,
\"status\": \"success\",
}"
| rex "UPDATE_COUNT\": \"(?<UPDATE_COUNT>\d+)"
| rex "DELETE_COUNT\": \"(?<DELETE_COUNT>\d+)"
Basically, I have a retrieved values from a DynamoDB using Powershell and have gotten a row in JSON format like the following
function gettagsfromdynamo() {
$table_name = "table_name"
$dbkey = '{"ReleaseId":{"AttributeValueList":[ {"N":"1"} ],"ComparisonOperator": "EQ"}}' | ConvertTo-Json -Compress
$dbvalue = aws dynamodb query --table-name $table_name --key-conditions $dbkey --region $region
$latest_tags = $dbvalue
$latest_tags
}
$db_tags = gettagsfromdynamo
This is what db_tags looks like
{
"Items": [
{
"Comment": {
"S": "The first PC release."
},
"Product": {
"S": "PC"
},
"ReleaseId": {
"N": "1"
},
"CreatedOn": {
"S": "12/14/2020 15:23:32"
},
"Tags": {
"S": "{\n \"software1\": \"software1.zip\",\n \"software2\": \"software2.zip\",\n \"software3\":\n [\n \"software3.zip\",\n \"software4.zip\",\n \"software5.zip\"\n ],\n \" data1 \": \"2020_NA\",\n \" 2020_EU \": \"20201_EU\",\n \" 2020_WW \": \"2021_WW\",\n \" dataversions\":\n [\n \"2020\",\n \"2019\",\n \"2018\",\n \"2017"\n ],\n \" products \": \" \"\n}"
}
}
],
"Count": 1,
"ScannedCount": 1,
"ConsumedCapacity": null
}
The task I want to achieve is to be able to get the "dataversions" value which is --> [Items][Tags][Dataversions] and write that value to a JSON file available locally. I have tried various things including using the Convert-ToJson and ConvertFrom-Json.
The tags json value looks like this without the escaped spaces (/n)
{
"software1": "software1.zip",
"software2": "software2.zip",
"software3":
[
"software3.zip",
"software4.zip",
"software5.zip"
],
" data1": "2020_NA",
" 2020_eu ": "2020_EU",
" 2020_ww": "2021_WW",
" dataversions":
[
"2020",
"2019",
"2018",
"2017"
],
" products ": " "
}
How do I retrieve the value of 'dataversions', which is a list of strings. Right now, I can only get it like this after using various tries:
{"S":"{\n \"software1\": \"software1.zip\",\n \"software2\": \"software2.zip\",\n \"software3\":\n [\n \"software3.zip\",\n \"software4.zip\",\n \"software5.zip\"\n ],\n \" data1\": \"2020_NA\",\n \" 2020_EU\": \"20201_EU\",\n \" 2020_WW\": \"2021_WW\",\n \" dataversions\":\n [\n \"2020\",\n \"2019\",\n \"2018\",\n \"2017\"\n ],\n \" products\": \" \"\n}"}
I want to be able to get the value of dataversions inorder to overwrite another 'dataversions' which is inside example.json file. How do I get to the dataversions value and also clean up the \n?
In your JSON file, the property name " dataversions" contains a leading space. When the JSON string is converted to a custom object (via ConvertFrom-Json), the space will be included in the property name. Therefore it must be considered when using member access (object.property) syntax:
($db_tags | ConvertFrom-Json).' dataversions'
Hey I am using conduit curl method to create tasks from post. It work fine when I run from terminal with hardcoded values. But when I try to execute it with variables it throws an error:
Script:
#!/bin/bash
echo "$1"
echo "$2"
echo "$3"
echo "$4"
echo "$5"
echo '{
"transactions": [
{
"type": "title",
"value": "$1"
},
{
"type": "description",
"value": "$2"
},
{
"type": "status",
"value": "$3"
},
{
"type": "priority",
"value": "$4"
},
{
"type": "owner",
"value": "$5"
}
]
}' | arc call-conduit --conduit-uri https://mydomain.phacility.com/ --conduit-token mytoken maniphest.edit
execution:
./test.sh "test003 ticket from api post" "for testing" "open" "high" "ahsan"
Output:
test003 ticket from api post
for testing
open
high
ahsan
{"error":"ERR-CONDUIT-CORE","errorMessage":"ERR-CONDUIT-CORE: Validation errors:\n - User \"$5\" is not a valid user.\n - Task priority \"$4\" is not a valid task priority. Use a priority keyword to choose a task priority: unbreak, very, high, kinda, triage, normal, low, wish.","response":null}
As you can see in error its reading $4 and $5 as values not variables. And I am failing to understand how to use $variables as input in these arguments.
You're using single quotes around the last echo to so that you can use double-quotes inside the JSON, but that causes echo to print the string without expanding anything. You need to use double quotes for the string, so you'll have to escape the double quotes inside of it.
Replace the last echo with this:
echo "{
\"transactions\": [
{
\"type\": \"title\",
\"value\": \"$1\"
},
{
\"type\": \"description\",
\"value\": \"$2\"
},
{
\"type\": \"status\",
\"value\": \"$3\"
},
{
\"type\": \"priority\",
\"value\": \"$4\"
},
{
\"type\": \"owner\",
\"value\": \"$5\"
}
]
}"
and it'll work. To avoid issues like this you can check http://wiki.bash-hackers.org and http://mywiki.wooledge.org/BashGuide for some general tips for bash newbies. Also, you can use shellcheck with a lot of text editors, which will spot errors like this automatically.