JSON parsing -SyntaxError: Unexpected token in JSON at position 72 - json

I have generate several one-liner JSON files(from excel) -which i attempted to read via the cordova mobile app. The file is being read but the problem is parsing it
example onliner Json File: Note the file is being read
inside onloadend:
{"name":"Acrobat Ant","id":"500","category":"Ants","features":{"format": {"cn":"hd_lst" ,"fm":"inline","edit": 0},"npma":"1.Ants//13.Wood-infesting Insects","compare2":"","length":"Adults 2.5-4mm // Queen 10mm","color":"Brown to black // "... (this is only partial
Error in Success callbackId: File899903995 : SyntaxError: Unexpected token   in JSON at position 72
cordova.js:310 SyntaxError: Unexpected token   in JSON at position 72
at JSON.parse (<anonymous>)
at FileReader.reader.onloadend (plugins.js:188)
at FileReader.readSuccessCallback (plugins/cordova-plugin-file/www/FileReader.js:164)
at Object.callbackFromNative (cordova.js:291)
at <anonymous>:1:9
This happens to ever single file i try. That position 72 (for this file) is at the end of the {"format"
word just before the colon :
As i said this happens to every other file except the position changes but all fail exactly right at the end of the format property.
I used https://jslint.com/ to check the syntax of the one line JSON and it says is good.
The curious thing is that if the one liner is changed to a layered format(via visual Code Shift-ALt-F)
as the sample breakdown shown below the JSON gets parse with no problem.
{
"name": "Acrobat Ant",
"id": "500",
"category": "Ants",
"features": {
"format": {
"cn": "hd_lst",
"fm": "inline",
"edit": 0
},
....etc
By the way The one line JSON files were generated from Excel and saved as UTF8 Without BOM. (It fails if i try to parse used it WITH BOM.)
So My dilemma is That I have over 200 files which I would like them to be read immediately once created without I having to do the manual extra step to turn them into a layered format JSON.
FYI I used exactly the suggestion on how to save excel data to UTF8 without BOM from
VBA : save a file with UTF-8 without BOM
please advise

Related

Snowflake throwing error (Error parsing JSON: misplaced { )

I am trying to load json files in to snow flake using copy command.I have two files of same structure.However one file loaded without issue,the other one is throwing the error
"Error parsing JSON: misplaced { "
The simple example select parse_json($1) record from values ('{{'); also errors with Error parsing JSON: misplaced {, pos 2 so your second file probably does in fact contain invalid JSON.
Try running the statement in validation mode (e.g. copy into mytable validation_mode = 'RETURN_ERRORS';) which will return a table containing useful troubleshooting info like the line number and character of the error(s).
The docs cover this here: https://docs.snowflake.com/en/sql-reference/sql/copy-into-table.html#validating-staged-files

Excel - We found extra characters at the end of JSON input

I am trying to import my json file to excel via the get data function. When doing this i get an error saying that "We found extra characters at the end of JSON input"
i ran the json file in jsonformatter and got this additional piece of information:
Parse error on line 1:
...s":"1555615338756"}
{"created_at":"Thu A
-----------------------^
Expecting 'EOF', '}', ',', ']', got '{'
Edit: line 1
{"created_at":"Thu Apr 18 19:22:18 +0000 2019","id":1118957948263206913,"id_str":"1118957948263206913","text":"Arsenal jersey looks weird. #NapoliArsenal","source":"\u003ca href=\"https://mobile.twitter.com\" rel=\"nofollow\"\u003eTwitter Web App\u003c/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":955479430377373696,"id_str":"955479430377373696","name":"Yash Iyer","screen_name":"MesutOziI28","location":"Bengaluru South, India","url":null,"description":"RM,Nerazzurri,BFC,RCB,bcci,rafa nadal and so on! Lately into B99,superstore! Sympathetic liker of tweets!","translator_type":"none","protected":false,"verified":false,"followers_count":258,"friends_count":454,"listed_count":0,"favourites_count":47788,"statuses_count":5318,"created_at":"Mon Jan 22 16:37:02 +0000 2018","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"F5F8FA","profile_background_image_url":"","profile_background_image_url_https":"","profile_background_tile":false,"profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http://pbs.twimg.com/profile_images/1109886916609007616/9rAavtGh_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/1109886916609007616/9rAavtGh_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/955479430377373696/1544903252","default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"NapoliArsenal","indices":[28,42]}],"urls":[],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1555615338756"}
Forever old, but I was trying to ingest an AWS CloudCheckr JSON into Excel via Power Query and getting the "We found extra characters at the end of JSON input."
Finally figured out, with the help of https://jsonformatter.org/ that some data was provided as True -- without quotes, which Excel PQ needed. Simple find/replacing :True, with :"True", did the trick.
Seriously, Microsoft, you did not recognize Boolean when you found it? Excel would have had no problem.
The answer is in the error message:
Expecting 'EOF', '}', ',', ']', got '{'
Looking at where { appears notice that directly before that is }. The JSON has no separator after the closing curly } and thus cannot process it because it's looking for one of the following:
EOF
}
,
]
In this case, it most likely needs a ,. It could also need ],, if it's an array of items. If neither of those fix it, you will need to post the entire line 1 of your JSON.
I had to do two changes to make this work: changed json body to Pascal case notation even thought it was in Camel case in chrome payload
, and secondly I was returning a simple string to verify the service is working - once I changed that to an object (as a json formatted response) this resolved the error I was getting.

How to import Google Maps API into PostgreSQL?

I am trying to transfer data from a JSON file produced by the Google Maps API onto my PostgreSQL database. This is done through cURL and I made sure that the permissions have been correctly set.
The url:
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=London&destinations=Paris&key=AIza-[key-redacted]-3z6ho-o
The query:
copy bookings.import(info) from program 'C:/temp/mycurl/curl "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=London&destinations=Paris&key=AIzaSyBIhOMI68hTIFarH4jrb_eKUmvY3z6ho-o" --insecure'
However, when I try to do this on my table with column 'info' of type 'json', I get the following error:
ERROR: invalid input syntax for type json DETAIL: The input string
ended unexpectedly. CONTEXT: JSON data, line 1: { COPY import, line
1, column info: "{"
********** Error **********
ERROR: invalid input syntax for type json SQL state: 22P02 Detail: The
input string ended unexpectedly. Context: JSON data, line 1: { COPY
import, line 1, column info: "{"
I am trying to not include things such as PHP or any other tool currently, yet if the only option is that I would certainly consider it.
What exactly do you guys think I am doing wrong? Is it the syntax, the format or am I missing something?
Thanks!
COPY assumes that each newline indicates a new record. Unfortunately, the Google Maps DistanceMatrix API is pretty-printing your response which means that it comes through as 23 rows, none of which are valid JSON.
You can get around this by piping the curl response through something like jq.
copy imports(info) from program 'curl "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=London&destinations=Paris&key=<my_key>" --insecure | /usr/local/bin/jq "." -c'
jq has lots of useful features if you want to massage the response a bit more before stashing it in the database.

How to load OSM (GeoJSON) data to ArangoDB?

How I can load OSM data to ArangoDB?
I loaded data sed named luxembourg-latest.osm.pbf from OSM, than converted it to JSON with OSMTOGEOJSON, after I tried to load result geojson to ArangoDB with next command: arangoimp --file out.json --collection lux1 --server.database geodb and got hude list of errors:
...
2017-03-17T12:44:28Z [7712] WARNING at position 719386: invalid JSON type (expecting object, probably parse error), offending context: ],
2017-03-17T12:44:28Z [7712] WARNING at position 719387: invalid JSON type (expecting object, probably parse error), offending context: [
2017-03-17T12:44:28Z [7712] WARNING at position 719388: invalid JSON type (expecting object, probably parse error), offending context: 5.867441,
...
What I am doing wrong?
upd: it's seems that converter osm2json converter should be run with option osmtogeojson --ndjson that produce items not as single Json, but in line by line mode.
As #dmitry-bubnenkov already found out, --ndjson is required to produce the right input for ArangoImp.
One has to know here, that ArangoImp expects a JSON-Subset (since it doesn't parse the json on its own) dubbed as JSONL.
Thus, Each line of the JSON-File is expected to become one json document in the collection after the import. To maximize performance and simplify the implementation, The json is not completely parsed before sending it to the server.
It tries to chop the JSON into chunks with the maximum request size that the server permits. It leans on the JSONL-line endings to isolate possible chunks.
However, the server expects valid JSON for sure. Sending the chopped part to the server with possibly incomplete JSON documents will lead to parse errors on the server, which is the error message you saw in your output.

Format for writing a JSON log file?

Are there any format standards for writing and parsing JSON log files?
The problem I see is that you can't have a "pure" JSON log file since you need matching brackets and trailing commas are forbidden. So while the following may be written by an application, it can't be parsed by standard JSON parsers:
[{date:'2012-01-01 02:00:01', severity:"ERROR", msg:"Foo failed"},
{date:'2012-01-01 02:04:02', severity:"INFO", msg:"Bar was successful"},
{date:'2012-01-01 02:10:12', severity:"DEBUG", msg:"Baz was notified"},
So you must have some conventions on how to structure your log files in a way that a parser can process them. The easiest thing would be "one log message object per line, newlines in string values are escaped". Are there any existing standards and tools?
You're not going to write a single JSON object per FILE, you're going to write a JSON object per LINE. Each line can then be parsed individually. You don't need to worry about trailing commas and have the whole set of objects enclosed by brackets, etc. See http://blog.nodejs.org/2012/03/28/service-logging-in-json-with-bunyan/ for a more detailed explanation of what this can look like.
Also check out Fluentd http://fluentd.org/ for a neat toolset to work with.
Edit: this format is now called JSONLines or jsonl as pointed out by #Mnebuerquo below - see http://jsonlines.org/
gem log_formatter is the ruby choice, as the formatter group, now support json formatter for ruby and log4r.
simple to get stated for ruby.
gem 'log_formatter'
require 'log_formatter'
require 'log_formatter/ruby_json_formatter'
logger.debug({data: "test data", author: 'chad'})
result
{
"source": "examples",
"data": "test data",
"author": "chad",
"log_level": "DEBUG",
"log_type": null,
"log_app": "app",
"log_timestamp": "2016-08-25T15:34:25+08:00"
}
for log4r:
require 'log4r'
require 'log_formatter'
require 'log_formatter/log4r_json_formatter'
logger = Log4r::Logger.new('Log4RTest')
outputter = Log4r::StdoutOutputter.new(
"console",
:formatter => Log4r::JSONFormatter::Base.new
)
logger.add(outputter)
logger.debug( {data: "test data", author: 'chad'} )
Advance usage: README
Full Example Code: examples