I am trying to import the following json file into MongoDB with Compass but I get an error:
Unexpected token � in JSON at position 0 while parsing near '�' in C:\Users\antoi\Downloads\restaurants.json
> 1 | �
| ^
Then I tried to test it from another source but it didn't work:
Error: Parse error on line 10:
...s Park Bake Shop"} { "_id": { "$oid"
----------------------^
Expecting 'EOF', '}', ',', ']', got '{'
is a quotation mark missing? But when I add a quotation mark it gives me:
Error: Parse error on line 10:
...is Park Bake Shop"}, { "_id": { "$oi
----------------------^
Expecting 'EOF', got ','
Use mongoimport
mongoimport --db myDb --collection restaurants --file restaurants.json
restaurants.json is not a valid JSON, not wrapped in array nor it has commas at the end.
https://docs.mongodb.com/manual/reference/mongodb-extended-json/
Related
I was trying to format my json in jsonformatter.org and I'm not sure why I'm getting the below error
Parse error on line 1:
...","topBrand":false}{"_id":{"$oid":"601c
----------------------^
Expecting 'EOF', '}', ',', ']', got '{'
[error][1]
Sample JSON:
{"_id":{"$oid":"601ac115be37ce2ead437551"},"barcode":"511111019862","category":"Baking","categoryCode":"BAKING","cpg":{"$id":{"$oid":"601ac114be37ce2ead437550"},"$ref":"Cogs"},"name":"test brand #1612366101024","topBrand":false}
{"_id":{"$oid":"601c5460be37ce2ead43755f"},"barcode":"511111519928","brandCode":"BUCKS","category":"Beverages","categoryCode":"BEVERAGES","cpg":{"$id":{"$oid":"5332f5fbe4b03c9a25efd0ba"},"$ref":"Cogs"},"name":"Starbucks","topBrand":false}
The JSON you posted contains two separate JSON documents, and is thus invalid. That's what the error is complaining about, it expected EOF (i.e. End of File), but it instead got the opening-brace from the second JSON document.
Either format them one at a time, or merge them into one document somehow.
For example, you could turn it into a list:
{
"data": [
{ ... },
{ ... }
]
}
Or a larger compound object:
{
"data1": { ... },
"data2": { ... }
}
I have the following script:
#!/bin/bash
CONFIG_RECORDER=`aws configservice describe-configuration-recorders`
NAME=$(jq -r ‘.ConfigurationRecorders[].name’ <<<“$CONFIG_RECORDER”)
echo $NAME
I am trying to retrieve the value for name from the following JSON object:
{
"ConfigurationRecorders": [{
"name": "default",
"roleARN": "arn:aws:iam::xxxxxxxxxxxx:role/Config-Recorder",
"recordingGroup": {
"allSupported": true,
"includeGlobalResourceTypes": true,
"resourceTypes": []
}
}]
}
When running the script, I am getting an error stating that jq could not open the file. That is because I am trying to pass in the result stored in a variable. How can I move past this? Below is the error:
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end
(Unix shell quoting issues?) at <top-level>, line 1:
‘.ConfigurationRecorders[].name’
jq: 1 compile error
NAME=$(jq -r '.ConfigurationRecorders[].name' <<<"$CONFIG_RECORDER")
<<< is known as here-string.
-r removes the quotes in the result.
[] in the jq query is necessary as ConfigurationRecorders is a JSON array.
$( … ) is just an other form of command substitution than backticks. I prefer this one as it is more readable to me.
I have a function in bash psql_json() that hits a postgres database and returns a json. I can not edit this bash function but it injects a postgres statement into this this query which is then sent to the db:
"select row_to_json(t)::json from ($arg) t;"
Where arg is some statement ie:
select * from table
However, this function returns a weirdly formatted json string as seen below:
{\"id\":1,\"firstName\":\"firstName1\",\"lastName\":\"lastName1\"}\n
{\"id\":2,\"firstName\":\"firstName2\",\"lastName\":\"lastName2\"}
The above output is what happens after running these statements:
local_fn="$(mktemp)"
psql_json 'select * from table' > "$local_fn"
cat "$local_fn"
Now when I try to put this json as is into jq, I get the following error:
cat json1.json | jq '.'
jq: error: syntax error, unexpected $end, expecting ';' or ')' (Unix shell quoting issues?)
I found this thread which seems to indicate the issue is that I am passing a string into jq which it doesnt like and is unable to parse, so I tried both:
cat json1.json | jq 'fromjson]'
cat json1.json | jq '[.[]|fromjson]'
and they both return
parse error: Invalid numeric literal at line 1, column 3
Is there any way I can get string representation of the json above into jq in a clean way to process it or would I need to clean/edit the string in bash?
You could fix the input using jq or a text-processing tool such as sed:
< weird.json jq -R 'sub("^";"\"") | sub("$";"\"") | fromjson | fromjson'
or
< weird.json sed -e 's/^/"/' -e 's/$/"/' | jq -R 'fromjson|fromjson'
With your input, the result in both cases is:
{
"id": 1,
"firstName": "firstName1",
"lastName": "lastName1"
}
{
"id": 2,
"firstName": "firstName2",
"lastName": "lastName2"
}
This question already has answers here:
Escape field name in jq that contains '#' and '-'? [duplicate]
(2 answers)
jq special characters in nested keys
(1 answer)
Closed 3 years ago.
Given this json file wtf.json:
{
"I-am-test-v2": {
"exist": true
},
"works": {
"exist": true
}
}
I can verify it has these keys via:
$ jq 'keys' wtf.json
[
"I-am-test-v2",
"works"
]
I can access works via:
$ jq .works wtf.json
{
"exist": true
}
yet I cannot select:
$ jq .I-am-test-v2 wtf.json
as that will yield in error:
jq: error: am/0 is not defined at <top-level>, line 1:
.I-am-test-v2
jq: error: test/0 is not defined at <top-level>, line 1:
.I-am-test-v2
jq: error: v2/0 is not defined at <top-level>, line 1:
.I-am-test-v2
jq: 3 compile errors
I assume it has to do with the special char -, yet I am unsure how to quote or escape it, as these attempts also fail with the same error:
jq ".I-am-test-v2" wtf.json
jq ."I-am-test-v2" wtf.json
or some different error via:
jq ."I\-am\-test\-v2" wtf.json
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.I\-am\-test\-v2
jq: 1 compile error
I also tried:
jq .["I-am-test-v2"] wtf.json
How am I supposed to access the key?
Your key contains the dash - that is interpreted as the substraction operator.
You need to tell jq that it's a string by double quoting your key.
If you do that in command line, enclose your jq "script" into single quote in order to avoid your shell interpreting any special characters.
<wtf.json jq '."I-am-test-v2"'
You can access it via proper enquotation via:
jq '.["I-am-test-v2"]' wtf.json
{
"exist": true
}
Then it also works with escaping:
jq ".[\"I-am-test-v2\"]" wtf.json
Note that you cannot reverse the quote style:
jq ".['I-am-test-v2']" wtf.json
jq: error: syntax error, unexpected INVALID_CHARACTER (Unix shell quoting issues?) at <top-level>, line 1:
.['I-am-test-v2']
jq: 1 compile error
Error: Parse error on line 1:
{"Index":{"_id":1}}
{ "altitde": 11887.1,
"callsign": "ABX2040 ",
----------------------^
Expecting 'EOF', '}', ',', ']', got '{'
This piece of text: {"Index":{"_id":1}} is a complete JSON object. After the last "}" your parser is expecting the end of the file, but you are adding a new object, starting with a new "{"
Check http://www.json.org/ to see how correct JSON must be conformed