I have two lists that look like this:
List("key1", "key2")
List("val1", "val2")
I'm trying to get it into this JSON-like format:
[{"key1":"val1"},{"key2","val2"}]
Right now, this is what I'm using:
val output = List(attrKeys.zip(attrValues).toMap)
But it gives me this:
[{"key1":"val1","key2":"val2"}]
How do I get this into a list of separate map objects?
attrKeys.zip(attrValues).map(Map(_))
Related
I have two JSON. In the both I have name. How can I get names from first JSON and add to array? Later I want do this same with second JSON later i want both array compare? How can I do this?
jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}];
jsonArray2 = [{'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}];
for example I want:
a = [doug, dofug] b = [goud, doaaug]
and later check if these are the same arrays
i don't know how can i do this in jmeter, help
To convert one array to another:
Add JSR223 PostProcessor as a child of the request which returns first JSON Array
Put the following code into "Script" area:
def builder = new groovy.json.JsonBuilder()
builder(com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(),'$..name').collect())
vars.put('array1', builder.toPrettyString())
That's it, you should be able to access the newly generated JSON Array as ${array1} where required
Repeat the same steps for the second JSON array.
There are several options on how to compare 2 JSON entities, depending on what you're trying to achieve you can go for:
Response Assertion
a JSR223 Assertion and a 3rd-party library like:
Jackson
Gson
JSONassert
I'm using this json content (I'm open to suggestions on better formatting here):
{"forwardingZones": [{"name": "corp.mycompany.com","dnsServers": ["192.168.0.1","192.168.0.2"]}]}
Note: we may add more items to this list as we scale out, both more IPs and more names, hence the "join(',')" in the end of the code below.
And I'm trying to loop through it to get this result:
corp.mycompany.com=192.168.0.1;192.168.0.2
Using this code:
forward_zones = node['DNS']['forward_zones'].each do |forwarded_zone|
forwarded_zone_name = forwarded_zone['name']
forwarded_zone_dns_servers = forwarded_zone['dns_servers'].join(';')
"#{forwarded_zone_name}=#{forwarded_zone_dns_servers}"
end.join(',')
This is the result that I get:
{"dnsServers"=>["192.168.0.1", "192.168.0.2"], "name"=>"corp.mycompany.com"}
What am i doing wrong...?
x.each returns x. You want x.map.
I am trying to build a binding in an amp-list. What I need to do is extract the first entry in the array. For instance my json array looks like this:
"Ids": [
"123",
"456"
]
The amp-binding is looking like this:
[src]="'http://any.com?id=' + {{Ids}}"
I want this to end up like:
src="http://any.com?id=123
But is looks like this without any manipulation
src="http://any.com?id=123,456
Looking at the white listed functions, I wondering if I should use Array to String for this.
I am using elephantbird project to load a json file to pig.
But i am not sure how i can define the schema at load. Did not find a description about the same.
data:
{"id":22522,"name":"Product1","colors":["Red","Blue"],"sizes":["S","M"]}
{"id":22523,"name":"Product2","colors":["White","Blue"],"sizes":["M"]}
code:
feed = LOAD '$INPUT' USING com.twitter.elephantbird.pig.load.JsonLoader() AS products_json;
extracted_products = FOREACH feed GENERATE
products_json#'id' AS id,
products_json#'name' AS name,
products_json#'colors' AS colors,
products_json#'sizes' AS sizes;
describe extracted_products;
result:
extracted_products: {id: chararray,name: bytearray,colors: bytearray,sizes: bytearray}
how i can give the correct schema to them (int,string,array,array) and how can i flatten array elements into rows?
thanks in advance
to convert json array to tuple:
feed = LOAD '$INPUT' USING com.twitter.elephantbird.pig.load.JsonLoader() AS products_json;
extracted_products = FOREACH feed GENERATE
products_json#'id' AS id:chararray,
products_json#'name' AS name:chararray,
products_json#'colors' AS colors:{t:(i:chararray)},
products_json#'sizes' AS sizes:{t:(i:chararray)};
to flatten a tuple
flattened = foreach extracted_products generate id,flatten(colors);
I have an array of arrays that I need to sort, but I'm having trouble getting it figured out. My main array (mainArr) looks like this:
mainArr = ({code:"1", date:"1/2/2001", status:"Active"},
{code:"2", date:"6/2/2004", status:"Terminated"},
{code:"3", date:"2/2/2003", status:"Transferred"},
{code:"4", date:"9/2/2003", status:"Active"});
I need to sort the mainArr by the dates in the objects. The list should end up like this:
mainArr = ({code:"1", date:"1/2/2001", status:"Active"},
{code:"3", date:"2/2/2003", status:"Transferred"},
{code:"4", date:"9/2/2003", status:"Active"}.
{code:"2", date:"6/2/2004", status:"Terminated"});
In most cases, you can use the sortOn method of Array. For instance, if you wanted to sort by 'code':
mainArr.sortOn("code");
This will sort the array using the code field of each object to determine the order.
However, as you wish to sort by dates (in a string format), sorting will give incorrect results (as ordering alphabetically and in date order are not the same). You could add a new property to each object in the array to make sorting easier, eg:
{code:"1", date:"1/2/2001", status:"Active"}
Adding the date in reverse order (sortableDate), it would become:
{code:"1", date:"1/2/2001", status:"Active", sortableDate:"2001/2/1"}
and you can then order with:
mainArr.sortOn("sortableDate");