Invalid JSON Expression - json

I am making use of non-static import
JsonPath jp = response.jsonPath();
System.out.println(jp.get("data?(#.id>14).employee_name").toString());
For a JSON as shown below:
{"status":"success","data":[{"id":"1","employee_name":"Tiger Nixon","employee_salary":"320800","employee_age":"61","profile_image":""},{"id":"2","employee_name":"Garrett Winters","employee_salary":"170750","employee_age":"63","profile_image":""}]}
When i am trying to run it , i am getting below error:
java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: expecting EOF, found '[' # line 1, column 31.
data[?(#.id>14)].employee_name
^
1 error
Can someone guide me why is this error being thrown ?

I doubt if that syntax is right, nonetheless, you should be using the below
Also note that the id is a string in your response so you will have to include it in quotes
js.get("data.find {it.id > '14'}.employee_name").toString();

Related

Parsing with JSON.parse() returns the wrong unexpected token

When writing JSON.parse("hi") getting the following error:
Uncaught SyntaxError: Unexpected token 'h', "hi" is not valid JSON
When writing JSON.parse("foo") getting the following error:
Uncaught SyntaxError: Unexpected token 'o', "foo" is not valid JSON
Why is f letter ignored?
JSON.parse("false") is acceptable, so it's reading the "f" as a potential start of the "false" string, but failing when the next character isn't "a".

python error on string format with "\n" exec(compile(contents+"\n", file, 'exec'), glob, loc)

i try to construct JSON with string that contains "\n" in it like this :
ver_str= 'Package ID: version_1234\nBuild\nnumber: 154\nBuilt\n'
proj_ver_str = 'Version_123'
comb = '{"r_content": {0}, "s_version": {1}}'.format(ver_str,proj_ver_str)
json_content = json.loads()
d =json.dumps(json_content )
getting this error:
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Dev/python/new_tester/simple_main.py", line 18, in <module>
comb = '{"r_content": {0}, "s_version": {1}}'.format(ver_str,proj_ver_str)
KeyError: '"r_content"'
The error arises not because of newlines in your values, but because of { and } characters in your format string other than the placeholders {0} and {1}. If you want to have an actual { or a } character in your string, double them.
Try replacing the line
comb = '{"r_content": {0}, "s_version": {1}}'.format(ver_str,proj_ver_str)
with
comb = '{{"r_content": {0}, "s_version": {1}}}'.format(ver_str,proj_ver_str)
However, this will give you a different error on the next line, loads() missing 1 required positional argument: 's'. This is because you presumably forgot to pass comb to json.loads().
Replacing json.loads() with json.loads(comb) gives you another error: json.decoder.JSONDecodeError: Expecting value: line 1 column 15 (char 14). This tells you that you've given json.loads malformed JSON to parse. If you print out the value of comb, you see the following:
{"r_content": Package ID: version_1234
Build
number: 154
Built
, "s_version": Version_123}
This isn't valid JSON, because the string values aren't surrounded by quotes. So a JSON parsing error is to be expected.
At this point, let's take a look at what your code is doing and what you seem to want it to do. It seems you want to construct a JSON string from your data, but your code puts together a JSON string from your data, parses it to a dict and then formats it back as a JSON string.
If you want to create a JSON string from your data, it's far simpler to create a dict with your values and use json.dumps on that:
d = json.dumps({"r_content": ver_str, "s_version": proj_ver_str})

__init__() got an unexpected keyword argument 'content_type' while dumping the response with application/json type in Django

Following are snippet :
record = {'polno':1,'custname':'abc','poltype':'TW'}
resp =json.dumps(record,content_type="application/json")
-->Got the error
json.dumps doesn't have a content_type argument. See the function's signature.
Just use json.dumps(record)

After parsing a value an unexpected character was encountered: ". Path '[0] Issue

I've been taking an error while deserialize json from file path.
My json model that comes from Model is (Model.GroupsJson) so:
[{\"GroupContent\":\"<p><img alt=\"\" src=\"http://localhost:56502/images/uploads/1471441332_flat-style-circle-add.png\" style=\"height:32px; width:32px\" /></p>\n\",\"Questions\":[{\"QuestionId\":\"57c3de87b6455e070800df0d\",\"Order\":\"0\"},{\"QuestionId\":\"57c91f85b6455e13b0646e7d\",\"Order\":\"0\"},{\"QuestionId\":\"57c8317ab6455e08b47ad839\",\"Order\":\"0\"}]},{\"GroupContent\":\"<p>aaaa</p>\n\",\"Questions\":[{\"QuestionId\":\"57c7d4bfb6455e1a08f17a3a\",\"Order\":\"1\"},{\"QuestionId\":\"57c3d753b6455e1840ccf8b5\",\"Order\":\"1\"}]}]
and after i try to deserialize json model :
List<GroupModel> groupList = JsonConvert.DeserializeObject<List<GroupModel>>(model.GroupsJSON)
And then this exception throws:
Additional information: After parsing a value an unexpected character was encountered: ". Path '[0].GroupContent', line 1, position 31.
What do I have to do? Should I encode backslash characters on model?

Error in fromJSON(commandArgs(1)) : unexpected character '''

I am calling a R script from Scala by using scala.sys.process. This script takes a command line argument in JSON format and processes it. I am using rjson's fromJSON function to store the JSON in a list.
This works very fine when I execute the R script from the command line:
$ ./dfChargerFlink.R '{"Id":"1","value":"ABC"}'
But when I call it from scala, I get the following error:
Error in fromJSON(commandArgs(1)) : unexpected character '''
Execution halted
This is the code I am using:
val shellCommand = "./dfChargerFlink.R '"+arg+"'"
return shellCommand !!
where arg is the JSON string.
You can notice that I have appended " ' " to both the sides of the JSON string as if I don't, I get this error:
Error in fromJSON(commandArgs(1)) : unclosed string
Execution halted
How can this be solved? Is it some bug?