XPath for Json String - json

I have JSON string like below :
{
"ConfigurationJsonResult":
[
2246,
2247,
2248,
2249,
2250,
2251,
2252,
2253,
2254,
2255
]
}
My problem is i want to get 1st value i.e 2246 using xPath.
I have tried using /ConfigurationJsonResult[1] but it gives me [2246,2247.....2255], i just want 2246. How to achieve it.

You can write a mapping function to convert xpath to jsonpath and use converted string to access the data.
function xpathToJsonPath(xpath) {
var jsonPath = xpath.replace("//","..");
//Simillarly other mappings
return jsonPath;
}
var data = jsonPath(object, xpathToJsonPath("$..author"));
Mappings are given the link you have shared - http://goessner.net/articles/JsonPath/
You can do plenty of improvements on this like extending this as a function to prototype etc.

Related

Node-RED parse json

I am trying to pull out the value "533"
{
"d": {
"ItemValue_1": 533
},
"ts": "2021-01-20T10:59:41.958591"
}
This does not work
var ItemValue_1 = msg.payload.ItemValue_1;
msg.payload = ItemValue_1;
return msg;
My result is unsuccessful
I was able to solve on my own, it works.
sensorMeasurement=JSON.parse(msg.payload);
msg.payload=sensorMeasurement.d;
var msgout = {payload : msg.payload.ItemValue_1}
return msgout;
The better way to do this is as follows:
Add a JSON node before the function node, this will turn a string payload in to a JSON object (assuming the string actually represents a JSON object).
Then if you are using a function node the following:
msg.payload = msg.payload.d.ItemValue_1;
return msg;
It is bad practice to create a new object as you did in your answer as it throws away any meta data that may be required later that is attached to the original object.
Rather than use a function node it would also be cleaner to use a change node and the Move mode to shift the msg.payload.d.ItemValue_1 to msg.payload

Length is not worked in JSON Extractor in Jmeter

I need to get the count of card from json file. For this I've used $.storedCards.cards.lenght
in JSON Extractor but it doesn't work. There is an error message:
Options AS_PATH_LIST and ALWAYS_RETURN_LIST are not allowed when using path functions!
After that I've tried JSR223 PostProcessor with next script on goovy
def jsonText = '''${AllCards}''' //${AllCards} has json value
def json = new JsonSlurper().parseText(jsonText)
log.info( "Json length---------->"+json.resource.size())
${CardsCount} = props.get("4") //vars.put(json.resource.size.toString())
but there is problem with set value to my variable. Or when i've created variable in Groovy it was impossible to use outside from script.
My json file
"storedCards":
{
"cards":
[
{
"CardId":"123",
"cardBrand":"Visa",
"lastFourDigits":"2968",
},
{
"CardId":"321",
"cardBrand":"Visa",
"lastFourDigits":"2968",
},
..........
],
How can i get the count of card and set to my Variables? what should i use for this?
Your JSON data seems to be invalid. Assuming you have the valid JSON like below, I'm answering your question.
{
"storedCards": {
"cards": [
{
"CardId": "123",
"cardBrand": "Visa",
"lastFourDigits": "2968"
},
{
"CardId": "321",
"cardBrand": "Visa",
"lastFourDigits": "2968"
}
]
}
}
You dont need to write Groovy code, you can resolve this using JSON Extractor. Instead of using length function, use JSON path predicate like this-
$.storedCards.cards[*]
Though Variable you used in JSON Extractor won't give the solution right away, another JMeter function helps - __RandomFromMultipleVars
Excerpt from documentation -
The RandomFromMultipleVars function returns a random value based on the variable values provided by Source Variables.
The variables can be simple or multi-valued as they can be generated by the following extractors:
Boundary Extractor
Regular Expression Extractor
CSS Selector Extractor
JSON Extractor
XPath Extractor
XPath2 Extractor
Multi-value vars are the ones that are extracted when you set -1 for
Match Numbers. This leads to creation of match number variable called
varName_matchNr and for each value to the creation of variable
varName_n where n = 1, 2, 3 etc.
So once you use the predicate, you will get the count in the yourVariableName_matchNr. Example:-
Hope this help.

Get json data from var

I gets following data in to a variable fields
{ data: [ '{"myObj":"asdfg"}' ] }
How to get the value of myObj to another variable? I tried fields.myObj.
I am trying to upload file to server using MEANjs and node multiparty
Look at your data.
fields only has one property: data. So fields.myObj isn't going to work.
So, let's start with fields.data.
The value of that is an array. You can see the []. It has only one member, so:
fields.data[0]
This is a string. You seem to want to treat it as an object. It happens to conform to the JSON syntax, so you can parse it:
JSON.parse(fields.data[0])
This parses into an object, so now you can access the myObj property.
JSON.parse(fields.data[0]).myObj
var fields = { data: [ '{"myObj":"asdfg"}' ] };
alert(JSON.parse(fields.data[0]).myObj);

Extract array to JSON

I have a query that extracts a document with a username and password as a filter. My return value is an array (It will return the elements in phoneBook). How do I turn this into a JSON object? Just returning a JSON from a query would be ideal.
db.users.find({userName:"mark", passWord:"test1234"}, {phoneBook:1,_id:0})
{ "phoneBook" : [ { "firstName" : "Rupert", "lastName" : "Styx", "phoneNumber" : "9147388152", "email" : "ruperstyx#gmail.com" } ] }
I'm still playing around with this string. I can't extract the attributes inside of phoneBook and turn it into a JSON
If you use php then you can use jsonencode($your_array) function for converting an array into json object and if you working on other language then just search function for converting an array into json.
This is snipt from my code (my app in Node.js)
collection.find({
"word": req.query.word
}).toArray(function(err, results) {
var res = JSON.stringify(results);
});
Simply use JSON.stringify(results)
I hope it works for you
By using Robomongo tool you will be able to see query results in list, table and json view!
Using robomongo:
db.getCollection('ModelName')
.find({})
.limit(20)
.map(function(model){
return model.toSource()
})

Flash: Parsing a JSON file that has a node called Class

Essentially I'm trying to parse JSON and assigning the results to variables, one of which is "var = JSON.class;" with class being what's returned in the JSON. However, flash won't let me parse it because it's called class which it uses to create new classes. Is there any workaround or will I not be able to grab this node?
I'm not sure if this is what you're asking, but if you have a property in your JSON object named "class", you should be able to parse it like as shown below.
// assuming JSON object looks like this, and is stored in a var named 'jsonData'
var jsonData:Object = { "id": 0, "class": "MyClassName", "values": [1,2,3] }
// trying to parse like this won't work w/keywords like 'class':
var parsedValue : String = jsonData.class;
// parse it with this way, using the square brackets:
var parsedValue : String = jsonData["class"];