We are working with a partner that requires some fields to appear at the beginning of a JSON document. We build a document before knowing what this initial data will be and have been unable to find a way to insert it at the beginning.
We have:
Json::Value json_message
json_message["singlestring"] = "blah blah blah"
then later a function gets this message
SomeFunction(Json::Value &json_message) {
Json::Value sub_json;
sub_json["one"] = "some sub 1";
sub_json["two"] = "some sub 2";
json_message["subobject"] = sub_json;
}
which results in
{
"singlestring":"blah blah blah",
"subobject":
{
"one":"some sub 1",
"two":"some sub 2",
}
}
but we need
{
"subobject":
{
"one":"some sub 1",
"two":"some sub 2",
},
"singlestring":"blah blah blah"
}
Is there a way to insert "subobject" to the beginning of the document, or to append the existing json_message to sub_json?
Thank you
That is not possible due to the internal representation of object members.
JsonCpp uses a std::map. When you serialize to string using FastWriter, if an object is found std::vector<JSONCPP_STRING> getMemberNames() is called, which puts in a vector the keys of the std::map.
To reach your goal you must modify getMemberNames(), assigning a "priority" to members of your liking (such as subobject).
Related
I am creating a composer from terraform where I want to pass a json as input variable
Terraform code:
software_config{
env_variables{
AIRFLOW_VAR_MYJSON ="{'__comment1__': 'This the global section', 'project_id':'testproject', 'gce_zone':'us-east1-c', 'gce_region':'us-east1','networkname':'vpc1', 'subnetwork':'https://www.googleapis.com/compute/v1/projects/testproject/regions/us-east1/subnetworks/subnet1'}"
}
}
I am trying to read the value of AIRFLOW_VAR_MYJSON in DAG , but it is not working as the value is not recognized as JSON.
I tried converting it and then deserializing it with following code:
JSONList = Variable.get("MYJSON")
jsonvar = json.dumps(JSONList)
setting_var = Variable.set("settings", jsonvar)
dag_config = Variable.get("settings", deserialize_json=True)
but it is not working.
I have also tried using
dag_config =json.loads(jsonvar)
then reading value as
project_id = dag_config["project_id"]
but I get error : "string indices must be integers"
Please suggest a way to resolve this.
NOTE : I know the gcloud command to set variables from json file but that is not working in my case as the project is in VPC and kubernetes clusters are giving timeout or handshake error, so I have ruled out use of this option
Valid JSON can only be " not '. Try switching the quotes.
A value can be a string in double quotes, or a number, or true or false or null, or an object or an array.
software_config{
env_variables{
AIRFLOW_VAR_MYJSON ="{\"__comment1__\": \"This the global section\", \"project_id\":\"testproject\", \"gce_zone\":\"us-east1-c\", \"gce_region\":\"us-east1\",\"networkname\":\"vpc1\", \"subnetwork\":\"https://www.googleapis.com/compute/v1/projects/testproject/regions/us-east1/subnetworks/subnet1\"}"
}
}
Or a little nicer way:
software_config {
env_variables {
AIRFLOW_VAR_MYJSON = jsonencode({
"__comment1__" = "This the global section",
"project_id" = "testproject",
"gce_zone" = "us-east1-c",
"gce_region" = "us-east1",
"networkname" = "vpc1",
"subnetwork" = "https://www.googleapis.com/compute/v1/projects/testproject/regions/us-east1/subnetworks/subnet1",
})
}
}
I need to loop through the customDimensions.
example: I have scenario like 1000 tags(json objects) inside the customDimensions like below
cust_0: array
cust_1: array
like 1000 tags inside the customDimensions I want to iterate through and get the JSON object is it possible loop in kusto queries?
sample data: As given sample data I stored below in customDimensions like that i have multiple rows, and in that, I want to combine(merge) the array0, array1, array2 how to write the query to merge the records array0 array1 array2 are dynamically generated columns
{
"sample1":"data",
"sample2":"data",
"sample3":"data",
"sample4":"daa",
"sample5":"data",
"sample6":"data",
"array0":[
{
"1":"0",
"2":"1",
"3":"1",
"4":"1 1",
"5":"1 1",
"6":"",
"7":"",
"8":"1(1)",
"9":"1",
"10":"1"
},
"array1": [
{
"1":"0",
"2":"1",
"3":"1",
"4":"1 1",
"5":"1 1",
"6":"",
"7":"",
"8":"1(1)",
"9":"1",
"10":"1"
}
]
},
}
{
"sample1":"data",
"sample2":"data",
"sample3":"data",
"sample4":"daa",
"sample5":"data",
"sample6":"data",
"array0":[
{
"1":"0",
"2":"1",
"3":"1",
"4":"1 1",
"5":"1 1",
"6":"",
"7":"",
"8":"1(1)",
"9":"1",
"10":"1"
},
"array1": [
{
"1":"0",
"2":"1",
"3":"1",
"4":"1 1",
"5":"1 1",
"6":"",
"7":"",
"8":"1(1)",
"9":"1",
"10":"1"
}
]
},
}
you may be able to achieve that using mv-expand or mv-apply.
if you're not sure how, please provide a sample data set (preferably, using the datatable operator), and the expected output for it, with a verbal description of the logic you want to implement
I have textbox1 field in asp.net and a text area to show count of records.
I want to count the records split by , in textbox1 but when textbox1 is empty text area is showing 1.
Here is the code.
int contacts = textbox1.Text.Split(',').Count();
textarea.Text = contacts.ToString();
String.Split always returns at least one string, if you pass string.Empty you will get one string which is the input string(so in this case string.Empty).
Documentation:
....
If this instance does not contain any of the characters in separator,
the returned array consists of a single element that contains this instance.
You have to check it, f.e. with string.IsNullOrEmpty(or String.IsNullOrWhiteSpace):
int contacts = 0;
if(!string.IsNullOrEmpty(textbox1.Text))
contacts = textbox1.Text.Split(',').Length;
Try this
int contacts = string.IsNullOrEmpty(string.textbox1.Text)? string.empty: textbox1.Text.Split(',').Count();
textarea.Text = contacts.ToString();
This is because even when textbox1.Text is an empty string, that's still treated as one item. You need to use StringSplitOptions.RemoveEmptyEntries so that empty entries are ignored when producing the result of calling Split:
var contacts = textbox1.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Count();
To decompose what you've written into individual statements, what you have is:
var items = textbox1.Text.Split(new char[] { ', ' }, StringSplitOptions.RemoveEmptyEntries);
var countOfItems = itemsFromText.Count();
If you look at items you'll see that it's an array of strings (string[]) which contains one entry for each item in the text from textbox1.Text.
Even if an empty string is passed in (i.e. textbox1 is empty) there's still one string to be returned, hence the fact that your code as written is returning 1, whereas in countOfItems where I've broken the code apart it will have 0 because of the use of StringSplitOptions.RemoveEmptyEntries.
The documentation on msdn of the String.Split overload that takes StringSplitOptions as a parameter has more examples and detail about this.
This might be a really easy one but I couldn't seem to find an answer anywhere
I'm trying to comment my code as follows
Session("test") = "JAMIE" _
'TEST INFO
& "TEST" _
'ADDRESS INFO
& "ADDRESS = TEST"
With the code above i'm getting the error
Syntax error
But when I remove the comments like so
Session("test") = "JAMIE" _
& "TEST" _
& "ADDRESS = TEST"
It works fine so my guess is that I cannot comment my code between the _ character.
Is there some way I can get around this as I'd like to comment my code ideally
The _ character is the line continuation. It means that the next line is interpreted as if it was on the same line.
So, putting a comment in the middle of the line is a syntax error.
Since you want a solution:
Either put a comment before the continued line or after it
As Tim Schmelter points out in his answer, you can construct the value that will go into the Session object before you put it into the Session object - you can do that is separate statements and comment those to your hearts content.
As Oded has mentioned, The _ character continues the line so you cannot comment between.
You could write:
Dim value = "JAMIE"
'TEST INFO
value &= "TEST"
'ADDRESS INFO
value &= "ADDRESS = TEST"
Session("test") = value
Because that may create separate strings internally just to comment them, you could use a StringBuilder here. You could show us what you're really tring to do, so that we can suggest a different approach(if you need to comment each "line" of a single variable, you should consider to redesign the way you assign the value to the variable).
System.Text.StringBuilder str = new System.Text.StringBuilder();
str.Append("JAMIE");
str.Append("TEST");//TEST INFO
str.Append("ADDRESS");//ADDRESS INFO
public string Test
{
get
{
return Convert.ToString(Session["TEST"]);
}
set
{
Session["Test"] = value;
}
}
Test = st.ToString();
I've got 3 types of documents in my db:
{
param: "a",
timestamp: "t"
} (Type 1)
{
param: "b",
partof: "a"
} (Type 2)
{
param: "b",
timestamp: "x"
} (Type 3)
(I can't alter the layout...;-( )
Type 1 defines a start timestamp, it's like the start event. A Type 1 is connected to several Type 3 docs by Type 2 documents.
I want to get the latest Type 3 (highest timestamp) and the corresponding type 1 document.
How may I organize my Map/Reduce?
Easy. For highly relational data, use a relational database.
As user jhs stated before me, your data is relational, and if you can't change it, then you might want to reconsider using CouchDB.
By relational we mean that each "type 1" or "type 3" document in your data "knows" only about itself, and "type 2" documents hold the knowledge about the relation between documents of the other types. With CouchDB, you can only index by fields in the documents themselves, and going one level deeper when querying using includedocs=true. Thus, what you asked for cannot be achieved with a single CouchDB query, because some of the desired data is two levels away from the requested document.
Here is a two-query solution:
{
"views": {
"param-by-timestamp": {
"map": "function(doc) { if (doc.timestamp) emit(doc.timestamp, [doc.timestamp, doc.param]); }",
"reduce": "function(keys, values) { return values.reduce(function(p, c) { return c[0] > p[0] ? c : p }) }"
},
"partof-by-param": {
"map": "function(doc) { if (doc.partof) emit(doc.param, doc.partof); }"
}
}
}
You query it first with param-by-timestamp?reduce=true to get the latest timestamp in value[0] and its corresponding param in value[1], and then query again with partof-by-param?key="<what you got in previous query>". If you need to fetch the full documents together with the timestamp and param, then you will have to play with includedocs=true and provide with the correct _doc values.