syntax error, unexpected keyword_end, expecting ')' [closed] - json

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a script called import.rb which will import json content from url to drafts directory.
require 'fileutils'
require 'json'
# Load JSON data from source
# (Assuming the data source is a json file on your file system)
data = JSON.parse('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec')
# Proceed to create post files if the value array is not empty
array = data["user"]
if array && !array.empty?
# create the `_drafts` directory if it doesn't exist already
drafts_dir = File.expand_path('./_drafts', __dir__)
FileUtils.mkdir_p(posts_dir) unless Dir.exist?(drafts_dir)
# iterate through the array and generate draft-files for each entry
# where entry.first will be the "content" and entry.last the "title"
array.each do |entry|
File.open(File.join(drafts_dir, entry.last), 'wb') do |draft|
draft.puts("---\n---\n\n#{entry.first}"
end
end
end
When I run ruby _scripts/import.rb I get _scripts/import.rb:20: syntax error, unexpected keyword_end, expecting ')'
end error. I changed line from recommended form to data = JSON.parse('https://script.google.com/macros/s/AKfycbyHFt1Yz96q91-D6eP4uWtRCcF_lzG2WM-sjrpZIr3s02HrICBQ/exec'). Original was data = JSON.parse(File.read(your_json_source.json)). Please assist me.

The problem is lacking ) at the end of 19 line:
...
array.each do |entry|
File.open(File.join(drafts_dir, entry.last), 'wb') do |draft|
draft.puts("---\n---\n\n#{entry.first}")
end
end
it should resolve the issue :)

Related

Remove the comma in the first column in .csv file using bash code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
I have a data as follows;
12432,20230
I want to remove the comma and replace it with space and want the output as follows;
12432 20230
I used the following code;
sed ’s/,/ /g’ file.csv > file.geno
but it gives error as;
sed: -e expression #1, char 1: unknown command: `�'
Your code is using "smart quotes", ’, instead of single quotes, ', just fix that and your syntax error will go away, i.e. instead of:
sed ’s/,/ /g’ file.csv > file.geno
use:
sed 's/,/ /g' file.csv > file.geno
You don't need the g at the end btw since you only have 1 comma in your input.

How to add an element to json file in perl [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
To change the md5 checksum of a json file, I am using the following filter in my httpd.conf:
ExtFilterDefine jsonfilter mode=output intype=application/json cmd="/usr/bin/perl -pe 'END { unless (-f q{/tmp/md5_filter.tmp}) { print qq(\\n\,\"STRING\"\: \") . time() . qq(\x0D\"\\n) }'"
But after the filter run, I receive an error, probally because the new string / timestamp get added after the last bracket } and leads to an unvalid json format:
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 224 column 2 of the JSON data
Does someone know how to get this run correctly? Just to be sure: I just want to change the output of the file, before it gets response to the client browser. I don't want to change the original file itself. This should be untouched.
probally because the new string / timestamp get added after the last bracket } and leads to an unvalid json format
I don't know any details of the environment that you're working in, but I suspect that your suspicion is correct here.
You could try to fix your string-manipulating solution (perhaps by working out when you're processing the final line of the input and then printing your addition before printing the closing brace. But the best solution is to gather the input into a single string and then process it using a JSON parser.
Something like this (untested):
perl -MJSON -ne '$json .= $_; END { my $data = decode_json($json); $data->{STRING} = time; print encode_json($data);'
Update: To explain the various parts of my code.
-MJSON - loads the JSON module
-ne - n iterates over the input (putting each line, in turn, into $_); e executes code from the command line
$json .= $_ - this is run for every line of the input. It just takes each line and gathers them all together in a variable called $json
END { ... } - this block of code is only run once - after the input has all been read
my $data = decode_json($json) - takes the JSON text string (now stored in $json) and decodes it into a Perl data structure. That data structure is stored in $data
$data->{STRING} = time - adds another key/value pair to the data structure
print encode_json($data) - encodes the altered data structure back into a JSON string and prints that string to STDOUT

Prettify a one-line JSON file [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I downloaded a 203775480 bytes (~200 MiB, exact size is important for a later error) JSON file which has all entries all on one line. Needless to say, my text editor (ViM) cannot efficiently navigate in it and I'm not able to understand anything from it. I'd like to prettify it. I tried to use cat file.json | jq '.', jq '.' file.json, cat file.json | python -m json.tool but none worked. The former two commands print nothing on stdout while the latter says Expecting object: line 1 column 203775480 (char 203775479).
I guess it's broken somewhere near the end, but of course I cannot understand where as I cannot even navigate it.
Have you got some other idea for prettifying it? (I've also tried gg=G in ViM: it did not work).
I found that the file was indeed broken: I accidentally noticed a ']' at the beginning of the file so I struggled to go to the end of the file and added a ']' at the end (it took me maybe 5 minutes).
Then I've rerun cat file.json | python -m json.tool and it worked like a charm.

JSON markup - SyntaxError: JSON Parse error: Unrecognized token '<' [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm getting one error in my code, Please suggest.
Google tool gives:
Syntax error: value, object or array expected.
JASON=LD Playground gives:
JSON markup - SyntaxError: JSON Parse error: Unrecognized token '<'
The latter error is due the fact that you left some HTML part inside the JSON validator text box (<script... is an HTML tag, it's not part of JSON).
The former error is due the fact that sometimes your JSON is using wrong double quotes symbols. Please note that double quotes can appear as:
" U+0022 QUOTATION MARK
“ U+201C LEFT DOUBLE QUOTATION MARK
” U+201D RIGHT DOUBLE QUOTATION MARK
Only the first symbol (that is the one that is present in almost all keyboards) is valid in JSON.

replace all whitespace in text file unless after # character [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to replace all whitespace in a text file with a \t so that I can load the contents into SQL. The problem that I'm having is that I only want to replace white space before the a string containing a mix of white space and other characters.
Before:
1.5e-61 5.3e-58 132 0.99 # 3 # 398 # -1 # ID=6412_1;partial=11;start_type=Edge;rbs_motif=None;rbs_spacer=None;gc_cont=0.394
After:
1.5e-61\t5.3e-58\t132\t0.99\t# 3 # 398 # -1 # ID=6412_1;partial=11;start_type=Edge;rbs_motif=None;rbs_spacer=None;gc_cont=0.394
I essentially want to keep everything after the # character as a string. Can anybody help please?
Parse, process, compose.
while (<>) {
chomp;
my ($data, $comment) = split(/(?=#)/, $_, 2);
$data =~ s/\s+/\\t/g;
print($data, $comment, "\n");
}