The data
{
"asdf": "1.0",
"jkadfsjkl": "xxx"
}
passes JSONLint but jqplay.org and jq v1.5 return
parse error: Invalid numeric literal at line 2, column 9
There are extra spaces in this line: "asdf": "1.0", .. I wonder if there might be an invalid character in there.
Related
I have error with scan function, why?
https://jqplay.org/s/E-0qbbzRPS
I need do this without -r
There are two issues with your filter. Firstly, you need to separate parameters to a function with semicolon ;, not comma ,:
scan("([0-9A-Za-z_]+) == '([0-9A-Za-z_]+)"; "g")
Secondly, scan with two parameters is not implemented (in contradiction to the manual).
jq: error: scan/2 is not defined at <top-level>, line 1:
But as you are using scan, your regex will match multiple occurrences anyway, so you may as well just drop it :
.spec.selector | [scan("([0-9A-Za-z_]+) == '([0-9A-Za-z_]+)") | {(.[0]): .[1]}]
[
{
"app": "nginx"
}
]
Demo
I have an output that i am getting in this format :-
[
{
"_class": "hudson.model.FreeStyleProject",
"name": "my-name",
"id": "123"
},
{
"_class": "hudson.model.FreeStyleProject",
"name": "my-name2",
"id": "456"
},
{
"_class": "hudson.model.FreeStyleProject",
"name": "my-name3",
"id": "789"
}
]
How can i parse the name and id using jq?
I tried to use [].name
but i get curl: (23) Failed writing body (320 != 1338)
Any help will be appreciated. Thank you.
You failed to mention the relevant error:
jq: error (at <stdin>:17): Cannot index array with string "name"
The program should be
.[].name
Because you provided an incorrect program to jq, it exited earlier than it normally would. This caused the pipe between curl and jq to close, which cause curl to become unable to write to the pipe, which caused curl to emit the error message you did provide.
Demo
https://jqplay.org/s/nolGbk3sD1
Use filter
.[] | .name, .id
I struggle with json file in robot framework:
testy.json
"A": {
"AA": "cacaca",
"AB": "cbcbcb"
},
"B": ["ea", "eb"],
"C": "aaa",
"D": "bbb",
"E": "ddd"
I tried to get all types in json file: 1st is dict, 2nd list, 3rd str.
The problem is when FOR loop pass "C" value ("aaa") in RF, it only pass aaa, which in Python is error - it skips quotation marks.
I need type of value, to make if statement later.
RF code: https://pastebin.com/WdbzXPcW
Cheers!
PS
It's my first question here, so "Hello World!" :D
You are close with your code.
All you have to do is not use the standard way variables are used in RF (${variable}), but use $variable instead:
${json_obj}= Load JSON From File file.json
${dict}= Convert To Dictionary ${json_obj}
${key_list} Get Dictionary Keys ${dict}
FOR ${key} IN #{key_list}
${value}= Set Variable ${dict['${key}']}
${type} Evaluate type($value)
Log To Console ${type}
END
this will give me:
<class 'dict'>
<class 'list'>
<class 'str'>
<class 'str'>
<class 'str'>
if I want a better-looking output, then:
${type} Evaluate type($value).__name__
which will give me:
dict
list
str
str
str
Also note that your json is not a valid json, so I added braces and made it valid (you can always check at https://jsonlint.com/):
{
"A": {
"AA": "cacaca",
"AB": "cbcbcb"
},
"B": ["ea", "eb"],
"C": "aaa",
"D": "bbb",
"E": "ddd"
}
There's "0" in a JSON giving me trouble, I'm trying to grab the value at:
response > data > sessions > 0 > user
The code I used was:
jq -r '.response.data.sessions."0".user'
The terminal returns: jq: error (at :1): Cannot index array with string "0"
I also tried:
jq -r '.response.data.sessions.0.user'
The terminal returned: jq: error: syntax error, unexpected LITERAL, expecting $end (Unix shell quoting issues?) at , line 1:
.response.data.sessions.0.user
Can use like this
https://jqplay.org/s/nYlPohfTdZ
.response.data.sessions[0].user
test.sh is not replacing test.json parameter value and gives jq compilation error.
test.json
{
"ParameterKey": "Project",
"ParameterValue": "<check>"
}
test.sh
cat test.json | jq \
'map(if .ParameterKey == "Project"
then . + {"ParameterValue" : "val" }
else . end)' > result.json
The reason you're seeing that message is that the map() expression is enumerating the values of your object ("Project" and "<check>") invoking the if .ParameterKey ... expression on each of them. Unfortunately those are strings and the .ParameterKey part of your if condition won't work with string values so jq gives you the error Cannot index string with string “ParameterKey”.
You probably only want the map() if your test.json contains an array of objects. If your test.json contains an object at the top level such as {"ParameterKey": "Project", "ParameterValue": "<check>"} then you should remove the map(). e.g.
$ cat test.json | \
jq 'if .ParameterKey == "Project" then . + {"ParameterValue" : "val" } else . end'
{
"ParameterKey": "Project",
"ParameterValue": "val"
}