Json Response - Accessing objects in an array - arrays

I'm attempting to access an object in an Json response, but not sure how. How can I access ID 11 using rest-assured, where ObjID1 and ObjID2 are unique UUID's?
"ObjID1": [
{
"ID": "11",
"NAME": "XYZ",
"GENDER": "M"
}
]
"ObjID2": [
{
"ID": "12",
"NAME": "Z",
"GENDER": "F"
}
]

To assert element's value you can use
then().body("ObjID1.ID[0]", equalTo("11"))
Indexing ID field with [0] allows you to get the ID of first JSON Object in the Array.
If you want to get this value for further processing then you can extract it like this:
JsonPath path = JsonPath.from("json file or json String");
List<HashMap<String, Object>> listOfJsonObjects = path.get("ObjID1");
We parsed the JSON and by using the path.get method we save Array of JSON Objects inside List of HashMaps. Each element in the list is the JSON Object.
In order to access first JSON Object you can use
HashMap<String, Object> jsonObject = listOfJsonObjects.get(0);
and then, using classic HashMap methods you can get specific element in the JSON Object like this:
jsonObject.get("ID");
The above will return "11"
Note that you will have to make a cast to String to get the value. Values in the HashMap are objects because JSON Objects in the array may contain nested Arrays or Objects.
String firstId = (String) jsonObject.get("ID");

Related

Combine JSON with same value into JSON array - Scala

I have converted a dataframe with columns email, account, id into json using toJSON. Each row is a JSON which looks like: {"email": "xyz", "account": "pqr", "id": "1"}.
The id field is not unique and I want to combine this array of JSON into array of JSON array such that each row is a array of JSONs with same id values.
For example: One row would look like: [{"email": "xyz", "account": "pqr", "id": "1"},{"email": "abc", "account": "lmn", "id": "1"}]
After this, I want to populate this JSON array into another dataframe user which has columns id and user.
The JSON array of each user with the matching id should be in the user dataframe.
O/p would be each row as: | 1 | [{"email": "xyz", "account": "pqr", "id": "1"},{"email": "abc", "account": "lmn", "id": "1"}] |
Can someone suggest how I can do this efficiently without exploding all the arrays multiple times?
I'm unsure which JSON library you are using, so I'd recommend to convert to a case class which has an id field. You could then group by the id field and then insert into your user dataframe, converting the grouped rows to JSON.
Something along the lines of...
case class Row(email: String, account: String, id: String)
val rows: List[Row] = ??? // converted from your dataframe
rows.groupBy(_.id)
.map { case (id, rows) =>
// insert into user dataframe. Convert rows to JSON
}

How to get an array inside of an object from findOne Mongoose

I am working with NodeJS and Mongoose. I have an object in MongoDB like this:
{
"loc": "idexample",
"a": ["1","2","3"],
"b": ["4","5","6"]
}
I want to get the value of the key "a" in array format, not in an object.
I am doing this:
element.findOne({loc: "idexample"}, function(err,doc){
console.log(doc.a); //Here I am getting an object like: 1,2,3 but I want an array, like this: ["1","2","3"]
});
The result is an object. I want an array. What can I do??

How to extract values out of a array using JSON Extractor in jmeter?

I want to extract below json and use values accordingly.
I/p JSON:-
{
"status": "Success",
"message": "User created successfully",
"id": [
131188,
131191
]
}
Here I want values of id field. I used JSON Extractor and gave expression as $.id which gives me [131188,131191] in a variable. Now I want to use individual values out of this array i.e. 131188 and 131191.
Any Idea how to do it?
Update : I don't want to use 2 JSON Extractors.
Just add [*] to your JSON path expression as below
$.id[*]
This will create a jmeter variable for each value.Note that you should use -1 in the match numbers field.
You could use a json extractor and a "JSR223 PostProcessor" with groovy language. An example:
import groovy.json.JsonSlurper
//String jsonString = vars.get("jsonFromExtractor")
String jsonString = '''
{
"status": "Success",
"message": "User created successfully",
"id": [
131188,
131191
]
}
'''
log.info("jsonString:" + jsonString)
def json = new JsonSlurper().parseText( jsonString )
String idValue1 = json.get("id").get(0)
String idValue2 = json.get("id").get(1)
log.info("idValue1:" + idValue1)
log.info("idValue2:" + idValue2)
I hope this helps

Json creation in ruby for a list

I am new to Ruby.
I want to create a JSON file for a group of elements.
For this, I am using eachfunction to retrieve the datas. I want to create json as follows for the 4 length array,
'{
"desc":{
"1":"1st Value",
"2":"2nd value"
"3":"3rd Value",
"4":"4th value"
},
}'
This is my array iteration,
REXML::XPath.each( doc, "//time" ) { |element1|
puts element1.get_text
}
I know here is the simple code to generate a JSON,
require 'json/add/core'
class Item < Struct.new(:id, :name); end
chair = Item.new(1, 'chair')
puts JSON.pretty_generate(chair)
This syntax will generate a json as follows,
{
"json_class": "Item",
"v": [
1,
"chair"
]
}
But I'm not sure how to do that to make JSON for my elements as stated above. Google search didn't give me a proper way to do this.
Can anyone help me here?
it means this?:
require 'json'
my_arr= ["1st Value","2nd Value","3rd Value","4th Value"]
tmp_str= {}
tmp_str["desc"] = {}
my_arr.each do |x|
tmp_str["desc"]["#{x[0]}"] = x
end
puts JSON.generate(tmp_str)
you can iterate the string array ,then take the strings to hash object.JSON can easy to parse Hash objcect .

Using JSON with an object

I have an object like
data: Array[1];
which can be accessed like
data[0].name
data[0].place
I am trying to convert this to JSON using like
var arr = JSON.stringify(data);
which returns
var arr = [{"name": "blah", "place": "ca"}]
But Im confused how to use this arr now its stringified ? How do I access for example the "name" value ? I tried arr.name but that doesn't seem to work ?
The array is
arr == [{"name": "blah", "place": "ca"}];
That object is the first item in the array
arr[0] == {"name": "blah", "place": "ca"}
and its properties...
arr[0].name == "blah"
your "name" is inside the hash/associative array which is inside an array
so u need to grab hash/associative array first, using
arr[0]
and then u can access your attributes.

Resources