Copy a JSON object - json

Let's say i have a JSON object:
myJson1={
key1:value1,
key2:value2
}
myJson2={};
and another json Object myJson2
When i run
myJson2 = myJson1;
All think goes well the first object is equal to the second.
But now if i try to use myJson2 like this,
var val = myJson2.key1;
console.log(val); is empty !!!
I have make some search and i found something about "proto"

Swagin9 is right - make sure your assignment statement is correct. I would comment on that post, but I don't have enough rep so I'll add this here - make sure value1 and value2 are defined. By themselves, they don't mean anything. Either put them in quotes, make them ints or booleans, etc. or define them as variables before instantiating myJson1 to make sure they actually mean something. Even with the correct assignment statement, you'll still get an error if your JSON objects are defined the way they are right now.
So for example, this produces an error:
myJson1={
key1: value1,
key2: value2
}
myJson2={};
myJson2 = myJson1;
console.log(myJson2.key1);
This does not produce an error:
myJson1={
key1: "value1",
key2: "value2"
}
myJson2={};
myJson2 = myJson1;
console.log(myJson2.key1);

It looks like you're using the wrong variable to assign to myJson2.
Should be
myJson2 = myJson1;

Related

How to access the key of a jsoncpp Value

I kind of feel stupid for asking this, but haven't been able to find a way to get the key of a JSON value. I know how to retrieve the key if I have an iterator of the object. I also know of operator[].
In my case the key is not a known value, so can't use get(const char *key) or operator[]. Also can't find a getKey() method.
My JSON looks like this:
{Obj_Array: [{"122":{"Member_Array":["241", "642"]}}]}
For the piece of code to parse {"122":{"Member_Array":["241", "642"]}} I want to use get_key()-like function just to retrieve "122" but seems like I have to use an iterator which to me seems to be overkill.
I might have a fundamental lack of understanding of how jsoncpp is representing a JSON file.
First, what you have won't parse in JsonCPP. Keys must always be enclosed in double quotes:
{"Obj_Array": [{"122":{"Member_Array":["241", "642"]}}]}
Assuming that was just an oversight, if we add whitespace and tag the elements:
{
root-> "Obj_Array" : [
elem0-> {
key0-> "122":
val0-> {
key0.1-> "Member_Array" :
val0.1-> [
elem0.1.0-> "241",
elem0.1.1-> "642" ]
}
}
]
}
Assuming you have managed to read your data into a Json::Value (let's call it root), each of the tagged values can be accessed like this:
elem0 = root[0];
val0 = elem0["122"]
val0_1 = val0["Member_Array"];
elem0_1_0 = val0_1[0];
elem0_1_1 = val0_1[1];
You notice that this only retrieves values; the keys were known a priori. This is not unusual; the keys define the schema of the data; you have to know them to directly access the values.
In your question, you state that this is not an option, because the keys are not known. Applying semantic meaning to unknown keys could be challenging, but you already came to the answer. If you want to get the key values, then you do have to iterate over the elements of the enclosing Json::Value.
So, to get to key0, you need something like this (untested):
elem0_members = elem0.getMemberNames();
key0 = elem0_members[0];
This isn't production quality, by any means, but I hope it points in the right direction.

Convert ResponseEntity<Any> to a List<ObjectNode> [Kotlin]

I have a method returning a ResponseEntity<Any> and taking a payload, which is of type ObjectNode. I am trying to do the following, but the cast is not correct:
val objectNodeList = responseEntity.body as List<ObjectNode>
How do you convert a ResponseEntity<Any> to a List<ObjectNode>?
Thank you
If the responseEntity.body is of type ObjectNode, then you can't cast it safely to anything else than that. If you really want a list from it, you can just make a simple list of one element with it:
val objectNodeList = listOf(responseEntity.body as ObjectNode)

Check and print occurrences of an array of string in a dataset in Python

I want to check if an array of strings occur in a dataset and print those rows where the string array elements occur.
rareTitles = {"Capt", "Col", "Countess", "Don", "Dr", "Jonkheer", "Lady",
"Major", "Mlle", "Mme", "Ms", "Rev", "Sir"}
dataset[rareTitles in (dataset['Title'])]
I am getting following error:
TypeError: unhashable type: 'set'
First of all, I think the comparison should go the other way around - you look for a dataset['Title'], that contains string from rareTitles.
You can use str attribute of a pandas DataSeries, which allows as to use string methods, like contains. As this method accepts also a pattern as a regular expression, you can put as an argument something like 'Capt|Col...'. To join all elements of a set you can use str.join() method.
So the solution would be
dataset[dataset['Title'].str.contains('|'.join(rareTitles))]
Link to documentation: pandas.Series.str.contains

Use string as a JSON command

I have this string:
var test = "toAdd";
I want to use it to extract data from JSON, like:
console.log(value.stats.test);
As you see, test is not correct, as it is just a string and can't be used, it is just not recognized at all. How do I make it recognize?
What you are attempting to do is this:
var someVar;
someVar.test = 'Sample';
someVar.test.attribute = 'Another sample';
// this:
console.log(someVar['test']['attribute']);
// will produce the same as this:
console.log(someVar['test'].attribute);
// as well as the same as this:
console.log(someVar.test['attribute']);
This will print "Another sample".
This has nothing to do with JSON. This is just javascript, and like anything where the . notation won't work switch to array notation:
foo.bar.baz = 'qux';
alert(foo['bar'].baz); // popup with 'qux'
^-----^-- note these
In your case, value.stats[test]. Now "test" isn't an array key, it's a variable whose value gets used as the key.

how to access first key from a map having list of values as object in coffee script

I am accessing this yaml file (converted to json) using a rest call.
A:
B:
C: [ value1, value2, value3 ]
D: [ value4, value5, value6 ]
looking at the json object on the uri, I see it being displayed like
{"C":["value1","value2","value3"],"D":["value4","value5","value6"]}
This result is expected, the way I am traversing the yaml file and sending it.
However, when I try to access the first key "C" of the map (yamlmap) using coffee script:
alphabet= (key for key,value of yamlmap)
It is not displaying anything. Is this the right way ?
As #mu-is-too-short have commented, your code is 'almost' right.
But I'd recommend below.
alphabet = Object.keys yamlmap
Furthermore explanation would be easily understandable by trying web compiler on http://coffeescript.org and comparing result of it.
In short,
alphabet = (key for key of yamlmap)
which would be better than your code (key for key,value of yamlmap) which is getting-and-ignoring value of the object, is still going to be equivalent in js this big below
alphabet = (function(){
var results = [];
for (key in yamlmap)
results.push(key)
return results;
})();
While the code I recommend would be almost the same in js like below and about 2 to 3 times faster than above.
alphabet = Object.keys(yamlmap);
Many template languages can have this kind of problem and we should be aware on that.