I am new to JSON. I have created custom JSON in AWS Opswork and trying to access it as an attribute in Chef recipe, but unfortunately its not catching the JSON values. My JSON file looks like..
{
"normal": {
"filebeat_minjar": {
"log_path" : "/var/log/*.log",
"hosts" : "Some Random Host ID",
"port" : 5000
}
}
}
and I am trying to catch it in recipe as,
log = node['filebeat_minjar']['log_path']
hosts = node['filebeat_minjar']['hosts']
port = node['filebeat_minjar']['port']
But it failed, I have also tried without 'normal'. I got the some []null class error.
Try this way,
log = node['normal']['filbeat_minjar']['log_path']
hosts = node['normal']['filbeat_minjar']['hosts']
port = node['normal']['filbeat_minjar']['port']
or
log = node.normal.filbeat_minjar.log_path
hosts = node.normal.filbeat_minjar.hosts
port = node.normal.filbeat_minjar.port
Json object is like a tree, the elements are the branches.
Hope this help
Your Chef code is correct, but you need to fix the JSON. You don't need the "normal": {...} in there, Chef and OpsWorks will handle that for you.
Following worked for me.
Custom JSON
{
"Production": {
"ApplicationLayer": {
"DockerTag" : "Version1"
}
}
}
Called from chef recipe.
node[:Production][:ApplicationLayer][:DockerTag]
Related
A JSON string string passes the jsonlint test.
response = [
{
"article" : {
"info" : {
"initial" : {
"articleIds" : [
"7461221587662919569"
],
}
},
"text" : "where they would 'transfer to' next.",
"lang" : "en",
}
},
{
"article" : {
"info" : {
"initial" : {
"articleIds" : [
"6613144915874808065"
],
}
},
"text" : "produto regional.",
"lang" : "pt"
}
}
]
However, after processing
require 'json'
file = File.read('/Users/main/jugg//article_samples.js')
data_hash = JSON.parse(file)
One is left with an array, whereas more frequently a hash with a name labels a subsequent array, where one works with that nomenclature such as response['data']
But in this case the array is not accessible via response[0]. How can this be considered as an array in order to process each individual element collection.each do |member|?
A curiosity: data_hash.class => NilClass
The response = ... code from article_samples.js is JavaScript, not JSON. This initializes a variable named response with a JavaScript array.
To use this as JSON, then rename the file to article_samples.json and remove response = from the file. The first line should start with [.
Now your second block of code should work just fine as long as the article_samples.json file is in the correct path.
On a side note, I suggest that you find a way to make the path more flexible. The way you have it currently hard coded is tied directly to your current machine's file system. This won't work if you want to run this code from another machine because the folder /Users/main/jugg probalby won't exist.
If this is a web server with ruby on rails, then one solution is to create an environment variable with the path where this file is stored.
I'm attempting to port an existing Functions app from core3.1 v3 to net5.0 I but can't figure out how to get the IOptions configuration pattern to work.
The configuration in my local.settings.json is present in the configuration data, and I can get to it using GetEnvironmentVariable. Still, the following does not bind the values to the IOptions configuration like it used to.
.Services.AddOptions<GraphApiOptions>()
.Configure<IConfiguration>((settings, configuration) => configuration.GetSection("GraphApi").Bind(settings))
The values are in the local.settings.json just as they were before:
"GraphApi:AuthenticationEndPoint": "https://login.microsoftonline.com/",
"GraphApi:ClientId": "316f9726-0ec9-4ca5-8d04-f39966bebfe1",
"GraphApi:ClientSecret": "VJ7qbWF-ek_Amb_e747nXW-fMOX-~6b8Y6",
"GraphApi:EndPoint": "https://graph.microsoft.com/",
"GraphApi:TenantId": "NuLicense.onmicrosoft.com",
Is this still supported?
What am I missing?
I had the same issue, but turns out that the json was not correctly formatted.
Just for reference, here it is how I configured it:
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(s =>
{
s.AddOptions<ApplicationSettings>().Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection(nameof(ApplicationSettings)).Bind(settings);
});
})
.Build();
And here is an example of local.setting.json:
{
"IsEncrypted": false,
"Values": {
"ApplicationSettings:test": "testtest",
"ApplicationSettings:testtest": "test"
}
}
I have a question, that's how to pass "--node-args" arguments in PM2 while using json config mode, like this:
pm2 start --node-args="--debug=5858" myPm2Config.json
well, I know I can write the arguments into myPm2Config.json file, but I dont want to do this, because I want to make two startup command as "debug" and "production" mode for launch application, such as "pm2_run" and "pm2_debug", and "pm2_debug" command with --node-args argument and "pm2_run" not, and I dont want to make two "myPm2Config.json" files, because that means if something needs changed, I will need to change two json config files, so, is there any easy way to do it? thanks guys!
I have found the solution! that's use js config instead of json config.
first, I create a pm2.config.js file. (mark: file name must be end with .config.js)
//[pm2.config.js]
let config = {
apps : [{
name : "node_shells",
script : "./bin/www",
log_date_format : "YYYY-MM-DD HH:mm:SS",
log_file : "logs/pm2.log",
error_file : "logs/pm2-err.log",
out_file : "logs/pm2-out.log",
pid_file : "logs/pm2.pid",
watch : true,
ignore_watch : ["logs/*", "node_modules/*", "uploads/*"]
}]
}
let debug_mode = false;
for(let arg of process.argv) {
if(arg == '-debug') {
debug_mode = true;
break;
}
}
if(debug_mode) {
console.log('== launching in debug mode ==');
config.apps[0].node_args = "--debug=5858";
}
else {
console.log('== launching in production mode ==');
config.apps[0].node_args = " "; //*require! or it will always uses latest debug options
}
module.exports = config;
then, create two launch files: "pm2_run" and "pm2_debug".
#[pm2_run]
pm2 start pm2.config.js
#[pm2_debug]
pm2 start pm2.config.js -- -debug
now, it's easy to switch debug mode or production mode!
I am a newbie in Neo4j and facing a small problem.
I created around 5000 router nodes with its ipaddress property set to specific ip.
Now I have around 5000 more interfaces and needs to create their nodes. I am using json and rest api for that in c++.
Every interface has a Routerip property and if the routerip matches the ipaddress of the router node that is already created I need to create that interface.
I have used this
http://docs.neo4j.org/chunked/snapshot/rest-api-cypher.html#rest-api-create-mutiple-nodes-with-properties link to create my routers.
Now I want to use the same method in order to create my interfaces.
Is there a way wherein I can do so passing array of properties as paramaters in the cypher query and check for the router present to create my interface?
There are several ways to do this. Breaking it down into steps:
For each interface, find the matching router
Create the interface & connect the interface to that router
That would look something like
MATCH (router:Router)
WHERE router.ipaddress = props.RouterIp
CREATE (n:Interface { props } )-[:CONNECTED_TO]->(router)
Hey When I try to run this simple query as above it gives me java exception
{
"params" : {
"props" : [
{
"LocalAsNumber" : 0,
"NodeDescription" : "10TiMOS-B-4.0.R2 ",
"NodeId" : "10.227.28.95",
"NodeName" : "BLR_WAO_SARF7"
}
]
},
"query" : "MATCH (n:Router) where n.NodeId = {props}.NodeId RETURN n"}
"message" : "The statement has been closed.",
"exception" : "BadInputException",
"fullname" : "org.neo4j.server.rest.repr.BadInputException",
"stacktrace" : [ "org.neo4j.server.rest.repr.RepresentationExceptionHandlingIterable.exceptionOnHasNext(RepresentationExceptionHandlingIterable.java:50)", "org.neo4j.helpers.collection.ExceptionHandlingIterable$1.hasNext(ExceptionHandlingIterable.java:46)", "org.neo4j.helpers.collection.IteratorWrapper.hasNext(IteratorWrapper.java:42)", "org.neo4j.server.rest.repr.ListRepresentation.serialize(ListRepresentation.java:71)", "org.neo4j.server.rest.repr.Serializer.serialize(Serializer.java:75)", "org.neo4j.server.rest.repr.MappingSerializer.putList(MappingSerializer.java:61)", "org.neo4j.server.rest.repr.CypherResultRepresentation.serialize(CypherResultRepresentation.java:83)", "org.neo4j.server.rest.repr.MappingRepresentation.serialize(MappingRepresentation.java:41)", "org.neo4j.server.rest.repr.OutputFormat.assemble(OutputFormat.java:215)", "org.neo4j.server.rest.repr.OutputFormat.formatRepresentation(OutputFormat.java:147)", "org.neo4j.server.rest.repr.OutputFormat.response(OutputFormat.java:130)", "org.neo4j.server.rest.repr.OutputFormat.ok(OutputFormat.java:67)", "org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:101)", "java.lang.reflect.Method.invoke(Method.java:606)", "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:132)", "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)", "java.lang.Thread.run(Thread.java:744)" ],
Using Drools 5.5.0.Final with Guvnor 5.5.0.Final with the sample mortgages package.
When submitting REST json request with the following Batch Execution Command:
{
"batch-execution": {
"lookup":"ksession1",
"commands":[
{
"insert":{
"out-identifier":"outApplicant",
"return-object":"true",
"object": {
"Applicant":{
"age":17
}
}
}
},
{
"fire-all-rules":""
}
]
}
}
returns: 500 Internal Server Error
com.thoughtworks.xstream.converters.ConversionException: Applicant : Applicant
---- Debugging information ----
message : Applicant
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : Applicant
class : org.drools.command.runtime.rule.InsertObjectCommand
required-type : org.drools.command.runtime.rule.InsertObjectCommand
converter-type : org.drools.runtime.help.impl.XStreamJson$JsonInsertConverter
line number : -1
class[1] : org.drools.command.runtime.BatchExecutionCommandImpl
converter-type[1] : org.drools.runtime.help..XSt...$JsonBatchExecutionCommandConverter
version : null
The Applicant class is defined in the mortgages package within an XSD like so:
age:Whole number (integer)
applicationDate:Date
creditRating:Text
name:Text
approved:True or False
How can I tell drools where to find the Applicant class? ( which is defined in the mortgage sample as an XSD file)
knowledge-services.xml currently looks like this:
<drools:grid-node id="node1"/>
<drools:kbase id="kbase1" node="node1">
<drools:resources>
<drools:resource type="PKG" source="http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/packages/mortgages"/>
</drools:resources>
</drools:kbase>
I suspect that changing the REST json request to fully specify the package name for Applicant class might work.
...
"object": {
"something.somethingelse.Applicant":{
"age":17
}
}
...
But can't seem to find where the fully qualified package name for Applicant is declared?
Acceptable answer must show an example which works without having to write java code, since the whole point of the REST interface is to access drools through a web service interface.
Is there a spring configuration , or some other way to write the json request that will work?
Since no one replied, I am posting the answer that worked for me along with the root cause and step-by-step procedure I used to debug the problem. Please do comment if there is a better way.
First, here is the complete and correct format for the REST Json request to insert an Applicant instance to the rules engine using drools-server when the model been defined in drools-guvnor GUI interface and not uploaded as a POJO model.
{
"batch-execution": {
"lookup":"ksession1",
"commands":[
{
"insert":{
"out-identifier":"outApplicant",
"return-object":"true",
"object": {
"mortgages.Applicant":{
"age":17
}
}
}
},
{
"fire-all-rules":""
}
]
}
}
Root cause: $TOMCAT_HOME/webapps/drools-server/WEB-INF/classes/knowledge-services.xml had incorrect resource.
The relevant parts of my corrected knowledge-services.xml:
<drools:grid-node id="node1"/>
<drools:kbase id="kbase1" node="node1">
<drools:resources>
<drools:resource
type="PKG"
source="http://localhost:8080/drools-guvnor/rest/packages/mortgages/binary"
basic-authentication="enabled"
username="admin"
password="admin"
/>
</drools:resources>
</drools:kbase>
Secondary issue: The authentication credentials were not specified in knowledge-services.xml
which resulted in this error:
Exception: java.io.IOException: Servier returned HTTP response code: 401 for URL: http://localhost:8080/drools-guvnor/rest/packages/mortgages/binary
Third issue: The example mortgage package was not built as a binary package in Guvnor.
ERROR: java.io.lang.RunTimeException: jav.io.StreamCorruptionException: Invalid Stream Header
To fix:
In Guvnor... Packages.. mortgages..Edit..build package
Additional note: INFO level logging is not enabled by default in drools-server.
To enable extra logging, so that you can see detailed debug messages in $TOMCAT_HOME/logs/catalina.log follow these steps:
Go to $TOMCAT_HOME/webapps/drools-server/WEB_INF/classes
create a file logging.properties
add these lines:
org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler
HTH