problem in accessing json parameters - json

I have to use json file like this:
({"weather":{"city":"Paris", "country":"France"}})
I write a JsonStore as:
root : "weather",
fields : ['city', 'country']
I cant access them like:
myStore.on('load', function(store, records, options) {
alert(records[0].get('city'));
}
but how can I access them because my json file doesn't have bracket
another case is when I want to access somthing like this:
({"data":{"weather":[{ "city" : "Paris", "country" : [{"value" : "France"}] }]}})
I define root as "data.weather"
fields:['city','country']
but I cant access value of country
please describe json file parsing or is there a way to load json file as plain/text?

I'm not familiar with extjs but I'll give this a shot since it doesn't seem to be very specific to the framework.
You can't index into records because you don't have an array in your JSON. I'd imagine if you used records.get('city'), you'd get Paris.

Related

Knime JSON transformer - Adding an attribute to a JSON object

I have converted some columns to JSON using the columns to json node. The output from that is:
{
"Material" : 101,
"UOM" : "GRAM",
"EAN" : 7698,
"Description" : "CHALK BOX"
}
I would like to add the value of the material property as a key to each JSON object. So, my desired output is:
"101": {
"Material" : 101,
"UOM" : "GRAM",
"EAN" : 7698,
"Description" : "CHALK BOX"
}
I have tried entering the following expression in the JSON transformer node but all I get is a question mark in the new column it generates:
$Material$:{"Material":$Material$,"UOM":$UOM$,"EAN":$EAN$,"Description":$Description$}
I have also tried replacing the $Material$ with "Material" but got the same result.
How would I go about this, please?
In case you convert the Material column to String (for example with String Manipulator), you can easily configure the Columns to JSON:
As you can see the Data bound key is the important part.
The String Manipulator node configuration (string($Material$)):
I finally managed to solve this by a different method.
I split the JSON data into several columns, then used the join function to create a string in the required order. I put the resulting string through the string to JSON node to create the new JSON object.
Thanks for all your tips and comments !

How to pass arguments directly from json file to keyword in robot framework?

I have a Json file like this.PFB the code:
"properties " : {
"xyz" : {
"username" : "abc#gmail.com",
"password" : "abc#123",
"phonenumber" : "1235",
},
"ABC" : {
"username" : "abc#gmail.com",
"password" : "abc#123",
"phonenumber" : "1345",
},
Keyword will be like :
Do operation for properties
[Arguments] ${username} ${password} ${phonenumber}
Log ${username}
Log ${password}
Log ${phonenumber}
My questions are :
1) The json file contains so many things but i have to fetch only properties from the file.How i will take properties part from the entire json file and pass directly arguments like username , password , phonenumber into keyword mentioned above.
2) How to write keyword for this logic such that we change only json file for adding more properties like apart from xyz, abc we will add as many properties we want and it will automatically fetch and keyword give us the desired result for all properties whatever we are modifying in json file.
If I understand you correctly, your question's summary is: a) how to read and parse a json file in Robotframework in this form, and b) pass each record's attributes to this keyword.
A file can be read from the file system with Get File.
One can read a json file with the python's json module, more specifically the loads() method - it takes a string, and returns a python object.
Your "json" sample is quite invalid json, so let's imagine that "properties" is somewhere (3 levels deep) inside the file.
${the file as string}= Get File c:\\the\\path\\to\\the\\file.json
${parsed}= Evaluate json.loads("""${the file as string}""") json
${properties}= Set Variable ${parsed["top"]["child"]["properties"]}
And now the variable properties is a dictionary, with those two keys - "ABC" and "xyz"; you just iterate over it, and pass the subkeys of each of the sub-dictionaries to the keyword.
FOR ${key} IN #{properties}
${sub dict}= Get From Dictionary ${properties} ${key}
Do operation for properties ${sub dict}[username] ${sub dict}[password] ${sub dict}[phonenumber]
END

How can i retrieve the "values" of json with R?

So i tried anything to get a specific key:value. But it seems that i cant find a solution for this. Any help?
{
"contracts":[
{
"contractid":"BemDRHtv17",
"cid":"",
"category":"WORK",
"mainCategory":"Grundeinkommen",
"configured":false,
"customMainCategory":null,
"customSubCategory":null,
"customContractPartner":null,
"amount":209200,
"interval":"MONTHLY",
"runTime":null,
"periodOfNotice":null,
"cancelationAlert":null,
"extensionPeriod":null,
"contractPartner":{
"creditorId":null,
"name":null,
"__typename":"ContractPartner"
},
"__typename":"Contract"
},
P.s i'm trying to access the key and value of mainCategory / search for its specific values.
You can access certain key using the following approach
using libraries like rjson , jsonlite
calling the json data into R using fromJSON() function. Lets say you saved the data like this
library(jsonlite)
jsonData = fromJSON("PATH") #Link to the file or html link
Now you can parse what you what using this approach and save the result in a matrix (or whatever data class you want to use)
variable <- as.matrix(jsonData$contracts$mainCategory)
Rather than converting your whole JSON to an R object, you can use library(jqr) to access specific elements of raw JSON
library(jqr)
jq(js, ".contracts[].mainCategory")
# "Grundeinkommen"
Data
js <- '{
"contracts":[
{
"contractid":"BemDRHtv17",
"cid":"",
"category":"WORK",
"mainCategory":"Grundeinkommen",
"configured":false,
"customMainCategory":null,
"customSubCategory":null,
"customContractPartner":null,
"amount":209200,
"interval":"MONTHLY",
"runTime":null,
"periodOfNotice":null,
"cancelationAlert":null,
"extensionPeriod":null,
"contractPartner":{
"creditorId":null,
"name":null,
"__typename":"ContractPartner"
},
"__typename":"Contract"
}
]}
'

Do I have to store ISODate in JSON for mongodb to work?

I have inserted a document into mongodb that looks like this
{ "_id" : ObjectId("5a0a532e09c1c52be0c14012"), "tranid" :
"414d", "startTime" : "2017-11-13T21:16:50.994Z","status" : "0" }
The "2017-11-13T21:16:50.994Z" is exactly how i receive into my java code as string, i just use
Document doc = Document.parse(jsonData);
to parse the string into Document and store in mongodb. Now i am trying to search
for documents greater than a certain date time like this
db.collection.find({"startTime":{$gte:new ISODate("2017-11-13T21:00:00.000Z")});
also tried
db.collection.find({"startTime":{$gte:new ISODate("2017-11-13T21:16:00Z")});
neither one seems to work. I did some look up online and saw that the JSON data needs to be ISODate, Should i convert the incoming startTime into ISODate first and then do the Document parse?

Display JSON Object from console

I have a JSON object returned in my console, and I want to display those data named "offers".
the JSON object is returned like that :
I tried to display my JSON Object data with :
console.log(JSON.stringify(data));
The thing is, it says that "data is not defined"
Does anyone know what happens ? :)
You should add full path to element of json, for example if your json looks like:
var json = {"par":22, "par2":555, "elems":[{"attr1":53, "attr2":99}] };
and if you want to get attr1 value, you should do something like this:
console.log(json.elems[0].attr1); // 53
so in your case that could be something like:
variableName.result.data.offers //variableName is variable that your "consoling"
Method JSON.stringify doesn't get yout specified value from JSON structure, it's converts JSON object to string.
console.dir provides a good representation of object than console.log().U can try with both
console.log(result.data.offers[0]);
console.dir(result.data.offers[0]);