Getting "Parse Error" on JSON code - At a Loss How To Fix - parsing

Error Message From JSON Checker:
Parse error on line 1:
< script type = "app
^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
My code on line 1:
< script type = "application/ld+json" > {
I'm no coder or programmer, just trying to dress up my website with some JSON enhancements and can't seem to find out what's wrong with line 1. Can anybody help?
I've looked all over the web for an answer and my code on line 1 appears to be correct.
I don't know what's wrong...

Is JSON Checker an external JSON Validator? To use it just try to validate what is inside of the script HTML element because that is the JSON. For example:
<script type = "application/ld+json">
{"Hello":"World"}
</script>
In this case you have to validate:
{ "Hello":"World" }
I hope this helps.

Related

Google Slide API script updateShapeProperties autofit

Getting error "GoogleJsonResponseException: API call to slides.presentations.batchUpdate failed with error: Invalid requests[3].updateShapeProperties: Invalid field: autofit_type"
but I think my code is right:
'updateShapeProperties': {
'objectId': pageElementId,
'fields': 'autofitType',
'shapeProperties': {
'autofit': {
'autofitType':'SHAPE_AUTOFIT'
}
}
}
Any help much appreciated
Cheers
Greg
I thought that the error message means that the value of fields is not correct. In your script, how about modifying as follows.
From:
'fields': 'autofitType',
To:
'fields': 'autofit.autofitType',
Note:
But, in the current stage, it seems that the value of autofitType can only use NONE. So, when SHAPE_AUTOFIT and TEXT_AUTOFIT is used for autofitType, an error of Autofit types other than NONE are not supported. occurs. Please be careful this.
About this, it seems that this is not reported as the future request in the issue tracker. So how about reporting it as the future request? Ref
When you test above modification, please modify 'autofitType':'SHAPE_AUTOFIT' to 'autofitType':'NONE'. By this, the request occurs no error.
Reference:
AutofitType
You're using an invalid field as what the error message says.
You can refer here for the available fields that can be used to replace your 'autofitType' field.
In response to Jason E. response, please see the documented Enum for AutofitType 1

Lua script - wrong CSS selector? "#tab-nav-main span:contains(desc)"

I'm using Scrapy-splash but this question is about Lua script. I need to wait until one element is visible. The problem is that this element can be navigated only using it's text.
The equivalent XPATH is: //*[#id="tab-nav-main"]//span[text()="desc"]')
I tried:
#tab-nav-main span:contains(desc)
which works in chrome but not in lua
What would you do?
function main(splash, args)
splash:set_user_agent(args.ua)
assert(splash:go(splash.args.url))
local i=0
local maxwait=5
while not splash:select("#tab-nav-main span:contains(description)") do
if i==maxwait then
break --times out at maxwait secs
end
i=i+1
splash:wait(1) --each loop has duration 1sec
end
return {
html = splash:html(),
}
end
ERROR
2019-03-14 17:07:05 [scrapy_splash.middleware] WARNING: Bad request to Splash: {'description': 'Error happened while executing Lua script', 'error': 400, 'info': {'splash_method': 'select', 'line_number': 9, 'error': 'cannot select the specified element {\'js_error\': \'Error: SyntaxError: DOM Exception 12\', \'js_error_type\': \'SyntaxError\', \'js_error_message\': \'SyntaxError: DOM Exception 12\', \'type\': \'JS_ERROR\', \'message\': "JS error: \'Error: SyntaxError: DOM Exception 12\'"}', 'source': '[string "..."]', 'message': '[string "..."]:9: cannot select the specified element {\'js_error\': \'Error: SyntaxError: DOM Exception 12\', \'js_error_type\': \'SyntaxError\', \'js_error_message\': \'SyntaxError: DOM Exception 12\', \'type\': \'JS_ERROR\', \'message\': "JS error: \'Error: SyntaxError: DOM Exception 12\'"}', 'type': 'SPLASH_LUA_ERROR'}, 'type': 'ScriptError'}

jsviews/jsrender Converter not working on {{:...}} tag

I am attempting to use a converter to modify a string in a jsrender template, but I can't seem to get the converter to work on a tag.
Using the example on JsRender API documentation Using converters for example, I have:
<script>
$.views.converters("upper", function(val) {
return val.toUpperCase();
});
</script>
Then in my HTML I have {{upper:Name}} which throws an error in the console: TypeError: val is undefined, and the template does not render at all.
However, if I apply the converter directly to a string like {{upper:"This should be uppercase"}} it outputs the string in uppercase as expected.
The {{:Name}} tag works fine by itself, so why isn't the converter working with it?
In case it is relevant, this is an ASP.NET-MVC project and the JSON data rendered by the template is coming from a $.post('#Url.Action(..,..)')... response. It's working perfectly until I attempt to apply the converter to the tag. Are converters not usable in this scenario?
It looks like your Name property is undefined in some case.
If you have a Name that is undefined, then {{:Name}} will render as the empty string, "" - but {{upper:Name}} will throw an error since undefined.toUpperCase() will fail.
You can investigate by breaking on thrown errors, (or by putting a break point or debugger statement in the converter) and seeing where your undefined Name is coming from.
You can also prevent the error getting thrown - and instead get error information rendered out - by any of the following techniques
write {{upper:Name onerror=true}} or {{upper:Name onerror='bad'}} (see https://www.jsviews.com/#onerror)
write
$.views.converters("upper", function(val) {
return val === undefined ? 'undefined' : val.toUpperCase();
});
write $.views.settings.debugMode(true);
and see what output you get, to investigate further about where your undefined Name is occuring.

JSON::ParserError: 757: unexpected token at '{

the current hash is
{\"report_name\"=>\"Study/Control: ABIRATERONE ACETATE - 20151413355\", \"left_mue_start_date\"=>\"02-26-2015\", \"left_mue_end_date\"=>\"03-19-2015\", \"right_mue_start_date\"=>\"02-26-2015\", \"right_mue_end_date\"=>\"03-19-2015\", \"report_formulary_id\"=>\",7581\", \"mue\"=>\"true\", \"mue_type\"=>\"study/control\", \"chain_id\"=>\"1\", \"left_mue_formulary_ids\"=>[\"7581\"], \"action\"=>\"create_report\", \"controller\"=>\"informatics\", \"user_id\"=>339}
now I need to convert it in proper hash like
{"report_name" => "Study/Control: ABIRATERONE ACETATE - 20151413355"}
so I am trying to get it with JSON.parse but I am getting error like:
JSON::ParserError: 757: unexpected token at '{
So if someone know about that so please help me.
and I am using Rails 3.2
What you have is a hash printed as String. To convert it into a Hash use eval.
ch = "{\"report_name\"=>\"Study/Control: ABIRATERONE ACETATE - 20151413355\", \"left_mue_start_date\"=>\"02-26-2015\", \"left_mue_end_date\"=>\"03-19-2015\", \"right_mue_start_date\"=>\"02-26-2015\", \"right_mue_end_date\"=>\"03-19-2015\", \"report_formulary_id\"=>\",7581\", \"mue\"=>\"true\", \"mue_type\"=>\"study/control\", \"chain_id\"=>\"1\", \"left_mue_formulary_ids\"=>[\"7581\"], \"action\"=>\"create_report\", \"controller\"=>\"informatics\", \"user_id\"=>339}"
hash = eval(ch)
# => {"report_name"=>"Study/Control: ABIRATERONE ACETATE - 20151413355", "left_mue_start_date"=>"02-26-2015", "left_mue_end_date"=>"03-19-2015", "right_mue_start_date"=>"02-26-2015", "right_mue_end_date"=>"03-19-2015", "report_formulary_id"=>",7581", "mue"=>"true", "mue_type"=>"study/control", "chain_id"=>"1", "left_mue_formulary_ids"=>["7581"], "action"=>"create_report", "controller"=>"informatics", "user_id"=>339}
PS: A JSON string should look as follows, meaning what you have is not JSON and hence you got JSON::ParserError for using JSON.parse on a non-JSON string :
"{\"report_name\":\"Study/Control: ABIRATERONE ACETATE - 20151413355\",\"left_mue_start_date\":\"02-26-2015\",\"left_mue_end_date\":\"03-19-2015\",\"right_mue_start_date\":\"02-26-2015\",\"right_mue_end_date\":\"03-19-2015\",\"report_formulary_id\":\",7581\",\"mue\":\"true\",\"mue_type\":\"study/control\",\"chain_id\":\"1\",\"left_mue_formulary_ids\":[\"7581\"],\"action\":\"create_report\",\"controller\":\"informatics\",\"user_id\":339}"
To avoid using eval you could use JSON.parse ch.gsub('=>', ':') this way you will get a HASH from your HASH stored as STRING
Last time when I got this issue, since the json file that I got from a API that contains a BOM
UTF-8 BOM is a sequence of bytes (EF BB BF)
What's different between UTF-8 and UTF-8 without BOM?
at the beginning of that string, but you know that part wouldn't display or as readable when we got the string from response. I try to use Ruby JSON to parse it, but I failed, I got the same exception with yours. just a reminder for others, when you get a Json response. By the way, this would be no problem while you are handling that in javascript, but with problems in Python or Ruby languages.
I ran into a similar problem, though it was failing while parsing \"\". This is in regards to using Pact.IO. I mention it here since this is the highest ranked Google result while looking for the error I encountered. The solution in my case was to change the body of a POST in my C# application so that it wasn't using empty string, but a null string. Basically I added this before my HTTP call.
if (request.Method == HttpMethod.Post && request.Content!=null && request.Content.GetType()==typeof(StringContent) && request.Content.ReadAsStringAsync().Result == String.Empty)
request.Content = new StringContent(null);

Why does to_json escape unicode automatically in Rails 4?

Rails 3:
{"a" => "<br/>"}.to_json
=> "{\"a\":\"<br/>\"}"
Rails 4:
{"a" => "<br/>"}.to_json
=> "{\"a\":\"\\u003Cbr/\\u003E\"}"
WHY???
It appears to be causing the error
Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8
When my Rails 3 app tries to parse JSON generated by my rails 4 app.
WHY???
To defend against a common weakness in web applications. If you say in an HTML page eg:
<script type="text/javascript">
var something = <%= #something.to_json.html_safe %>;
</script>
then you might think you're fine because you've JSON-escaped the data you're injecting into JavaScript. But actually you're not safe: aside from JSON syntax you also have surrounding HTML syntax, and in an HTML script block </ is in-band signalling. Practically, if #something contains the string </script> you've got a cross-site scripting vulnerability as this comes out:
<script type="text/javascript">
var something = {"attack": "abc</script><script>alert('XSS');//"};
</script>
The first script block ends halfway through the string (leaving an unclosed string literal syntax error) and the second <script> is treated as a new script block and the potentially-user-submitted content within it executed.
Escaping the < character to \u003C is not required by JSON but it is a perfectly valid alternative and it automatically avoids this class of problems. If a JSON parser rejects it, that is a severe bug in the reader.
What is the code that is producing that error? I'm not convinced the error is anything to do with the <-escaping, as it is talking about byte 0xC3 rather than 0x3C. That could be indicative of a string with UTF-8 encoded content not having been marked as UTF-8... maybe you need a force_encoding("UTF-8") on the input?
You can retain the original string with JSON::dump:
JSON::dump "a" => "<br/>"
=> "{\"a\":\"<br/>\"}"
JSON::dump "a" => "x&y"
=> {\"a\":\"x&y\"}" # instead of x\u0026y
Use it with care for the reasons bobince mentions and particularly avoid it with any user-generated input (or at least make sure that's sanitized).
Here's an example I encountered where it's a legitimate use. Generating a JavaScript hash argument in a helper function:
# application_helper.rb
def widget_js(post)
options = {
color: ColorCalculator(post.color).to_rgb_hex,
...
}
"third_party_widget(#{JSON::dump options});"
end
I encountered this issue too and as others have mentioned, it's caused by using the ActiveSupport to_json method. To resolve, use the JSON gem directly with JSON.generate(data) where data is an Array or Hash. See https://github.com/flori/json for all JSON gem documentation.
Was having a similar problem with Rails 7 sending "<" in JSON output like:
..., "legend":[{"text":"<96.8%","color":"#FFAFFF"},{"text":"96.8% to 98.8%","color":"#E37DE3"},{"text":"98.8% to 100%","color":"#BA50BA"}], ...
from something like:
{entry: dataset.entry, legend: dataset.legend, ...
The "<" sign was showing up "legend":[{"text":"\u003c96.8%", ...
In my case `JSON.generate({entry: ...})` fixed the issue

Resources