I try to find out why my JSON is not legal.
I use this site: http://jsonlint.com/
The first example which is good is:
{
"data": 1290,
"value": "a"
}
The second which is not good is:
{
"data": 1290,
"value": "a"
}
I dont understand why the second one does not work. Its the same as the first one.
EDIT
I found this sings at the end of the string.
How to remove them using PHP? the string source is from php.
There might be some additional output after you output your json. Try trimming output and terminating further execution.
$json = array('data' => 1290, 'value' => 'a');
echo json_encode($json);
die();
Related
I have the following item in Zabbix, with the current PreProcessing JSONPath:
Item key: vfs.fs.get
Preprocessing JSONPath script: $.[?(#.fstype =~ '{$FSTYPE.MATCHES}')]
The structured JSON list that it generates is the following:
[{
"fsname": "/",
"fstype": "ext4",
"bytes": {
"total": 30027956224,
"free": 9868255232,
"used": 18610745344,
"pfree": 34.650989,
"pused": 65.349011
},
"inodes": {
"total": 1872304,
"free": 1478095,
"used": 394209,
"pfree": 78.945246,
"pused": 21.054754
}
}, {
"fsname": "/mnt/dados",
"fstype": "ext4",
"bytes": {
"total": 157451501568,
"free": 149319151616,
"used": 62930944,
"pfree": 99.957872,
"pused": 0.042128
},
"inodes": {
"total": 9830400,
"free": 9830389,
"used": 11,
"pfree": 99.999888,
"pused": 0.000112
}
}]
So, currently it gets only items that have fstype "ext4" and a few others (Like ext3, etc.)
But in order to be more effective, i need to include another PreProcessing step that is one called "DOES NOT MATCH what is in regular expression."
So, the JSONPath script would be something like (Meaning "Is different from the regular expression", so items with empty values, weird numbers and space values will be ignored):
"$.[?(#.fstype != =~ '{$FSTYPE.DOES_NOT_MATCH}')]"
But i tried doing the following on a JSONPATH Evaluator app and i got the following error:
JSONPath: $.[?(#.fstype != =~ '^\s$')]
Error: jsonPath: Unexpected token '=': _$_v.fstype != =~ '^\s$'
The following JSONPath works but it does the opposite, it gets the items with empty values, weird numbers and space values and ignores others:
$.[?(#.fstype =~ '^\s$')]
Can someone help me? What am i missing?
Thank you...
Edit:
Progression:
I was able to create three PreProcessing steps, with the help of gregsdennis below.
JSONPath: $.[?(#.fstype =~ '^(ext4)$')]
JSONPath: $.[?(#.fsname =~ '.+')]
JSONPath: $.[?(!(#.fsname =~ '^(/dev|/sys|/run|/proc|.+/shm$)'))]
All steps above passed. But for some reason i am not able to negate the following expression:
$.[?(!(#.fstype =~ '^\s$'))]
Accordingly to RegEx documentation, the expression above means:
^ asserts position at start of the string
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
But its returning me the following error:
"Cannot extract value from json by path "$.[?(!(#.fstype =~ '^\s$'))]": unsupported construct in jsonpath starting with: "'^\s$'))]"
Edit 2:
I was able to fix the "unsupported construct in jsonpath starting with" error by escaping the backslash character. So:
$.[?(!(#.fstype =~ '^\s$'))]
There's not a single operator, but you should be able to negate the entire expression.
$.foo[?(!(#.bar =~ '[a-z0-9]+'))]
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": { ... }
}
Got the "syntaxError: JSON parse; unexpected non-whitespace character after JSON data at line 1 column 611 of the JSON data" error. While there are a lot of questions and answers about this and I see what is happening in my code, my question is:
Does this error originate with the construction of the array code here:
echo json_encode(array('html' => $html_string, 'cid' => $cid));
The input is:
$html_string = ['<div class="myclass">'.$value-> Customer_FName.'</div><div class="myclass">'.$value-> Customer_LName.'</div>'];
$cid[] = $value->id;
The output of the echo dumps as :
{"html":"<div class=\\\"myclass\\\">Bob<\/div><div class=\\\"myclass\\\">Smith<\/div>","cid":["44","45","46"]}0
Try escaping your special characters. Example:
{
"html": "<div class=\"flx2 waitlist-image waitlist-listing-img-pointer\">{$value-> Customer_FName}</div><div class=\"flx2 waitlist-image waitlist-listing-img-pointer\">{$value-> Customer_LName}</div>",
"cid": [
"44",
"45",
"46"
]
}
I need to loop through several jsons and print out index 1 of each...
I start with a generated string containing my JSON data. I then decode the string and dump it to show exactly what I'm working with:
my $decoded = decode_json $string
print Dumper $string
This results in the following output:
$VAR1 = [
{
'hdr' => [
1,
'acknowledged',
'',
'/home/clanier/dev/test/sds-test/data/JPLIDR2015169.64575',
'2015/271-19:10:39.0101355',
'2015/271-19:10:39.2599252',
''
]
},
{
'hdr' => [
2,
'acknowledged',
'',
'/home/clanier/dev/test/sds-test/data/JPLIDR2015169.64575',
'2015/271-19:10:39.3928414',
'2015/271-19:10:39.6397269',
''
]
},
{
'hdr' => [
3,
'acknowledged',
'',
'/home/clanier/dev/test/sds-test/data/JPLIDR2015169.64575',
'2015/271-19:10:39.7726375',
'2015/271-19:10:40.0162758',
''
]
}
];
Now I try to loop through this and print the word acknowledged for each one:
foreach my $hdr ( $decoded->{hdr} ) {
print $hdr->[1];
}
I looked to this solution for help, but it appears I cannot even get as far as the original poster, due to "Not a HASH reference" errors. I was able to print a specific one previously, but I need to loop through and print all of them. This was the code for that: print $$decoded[0]->{'hdr'}->[1];
$decoded is an array reference, not a hash reference. You can't dereference it as a hash: ->{.
You can dereference it as an array, though:
for my $hdr (#$decoded) {
print $hdr->{hdr}[1];
}
See perlreftut
for more information about references in Perl.
I've searched the forum and seen many folks with a similar problem, but not this exact one.
I think my question is the simplest form, and there must be something I'm missing because no one is asking it.
I have a shell script that calls a MongoDB script and gets the results in a file. I then want to parse that file with jq.
jq is breaking because the output from the query is not valid JSON. The offender is the ObjectId. I'm at a total loss as to how something that's "ALL JSON ALL THE TIME" produces invalid JSON.
I'm pretty sure there's something fundamental that I'm missing.
I have a file called MyMongoScript.js. Its contents look like this:
db.WorkflowJobs.find().sort({"STATUS":1}).forEach(printjson)
I call MyMongScript.js with the following command:
mongo -u $MONGO_U -p $MONGO_P $MONGO_DB -quiet --eval "var DATE_TO_RUN=\"$DATE_TO_RUN\"" MyMongoScript.js
Here's the results to STDOUT:
{
"_id" : ObjectId("52816fd50bc9efc3e6d8e33f"),
"WORK_TYPE" : "HIVE",
"Script_Name" : "upload_metrics_LANDING_to_HIST.sql",
"Stop_On_Fail" : true,
"STATUS" : "READY",
"START_TS" : "NULL",
"END_TS" : "NULL",
"DURATION" : "NULL",
"INS_TS" : "Mon Nov 11 2013 16:01:25 GMT-0800 (PST)"
}
Here's what jsonlint.com says about it:
Parse error on line 2:
{ "_id": ObjectId("52816fd50b
------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
Any help much appreciated.
Try this for your MyMongoScript.js:
db.WorkflowJobs.find().sort({"STATUS":1}).forEach(function(myDoc){myDoc._id=myDoc._id.valueOf();print(tojson(myDoc))});
The key is valueOf() which will set your ObjectId to a String.
EDITED Left out a paren.