Populating json objects from two separate lists - arrays

I have the following two lists:
['value1', 'value1', 'value1', 'value2', 'value2', 'value2', 'value3', 'value3', 'value3']
['value1_link1', 'value1_link2', 'value1_link3', 'value2_link1', 'value2_link2',
'value2_link3', 'value3_link1', 'value3_link2', 'value3_link3']
I am trying to populate json objects in the following manner:
[
{
"timestamp":"string",
"ext":"value1",
"discoveredlinks":['value1_link1', 'value1_link2', 'value1_link3']
},
{
"timestamp":"string",
"ext":"value2",
"discoveredlinks":['value2_link1', 'value2_link2', 'value2_link3']
},
{
"timestamp":"string",
"ext":"value3",
"discoveredlinks":['value3_link1', 'value3_link2', 'value3_link3']
}
]
Any help would be appreciated !
Thanks !

Looks to me like you don't need the first list, as all the info is in the second list. I can't guarantee that my solution is the most optimised, but one way you can do it is as follows:
import json
from collections import defaultdict
links = [
"value1_link1",
"value1_link2",
"value1_link3",
"value2_link1",
"value2_link2",
"value2_link3",
"value3_link1",
"value3_link2",
"value3_link3",
]
temp_dict = defaultdict(list)
for link in links:
ext = link.split("_")[0]
temp_dict[ext].append(link)
output = json.dumps(
[
{"timestamp": "string", "ext": ext, "discoveredlinks": discovered_links}
for ext, discovered_links in temp_dict.items()
]
)
print(output)
you can remove the the json.dumps if you'd rather have it as a list rather than a json string

Related

How can i get all values of a particular key from object array without loop in angular

[{"time":"2016-07-26 09:02:27","type":"aa"},
{"time":"2016-04-21 20:35:07","type":"ae"},
{"time":"2016-08-20 03:31:57","type":"ar"},
{"time":"2017-01-19 22:58:06","type":"ae"},
{"time":"2016-08-28 10:19:27","type":"ae"},
{"time":"2016-12-06 10:36:22","type":"ar"},
{"time":"2016-07-09 12:14:03","type":"ar"},
{"time":"2016-10-25 05:05:37","type":"ae"},
{"time":"2016-06-05 07:57:18","type":"ae"},
{"time":"2016-10-08 22:03:03","type":"aa"},
{"time":"2016-08-13 21:27:37","type":"ae"},
{"time":"2016-04-09 07:36:16","type":"ar"},
{"time":"2016-12-30 17:20:08","type":"aa"},
{"time":"2016-03-11 17:31:46","type":"aa"},
{"time":"2016-05-04 14:08:25","type":"ar"},
{"time":"2016-11-29 05:21:02","type":"ar"},
{"time":"2016-03-08 05:46:01","type":"ar"},
]
here I want only type key all values
like that
Please help me to do so thanks in advance to all.
Simplest way would be to use Array.map method like so:
let array = [{"time":"2016-07-26 09:02:27","type":"aa"},
{"time":"2016-04-21 20:35:07","type":"ae"},
{"time":"2016-08-20 03:31:57","type":"ar"},
{"time":"2017-01-19 22:58:06","type":"ae"},
{"time":"2016-08-28 10:19:27","type":"ae"},
{"time":"2016-12-06 10:36:22","type":"ar"},
{"time":"2016-07-09 12:14:03","type":"ar"},
{"time":"2016-10-25 05:05:37","type":"ae"},
{"time":"2016-06-05 07:57:18","type":"ae"},
{"time":"2016-10-08 22:03:03","type":"aa"},
{"time":"2016-08-13 21:27:37","type":"ae"},
{"time":"2016-04-09 07:36:16","type":"ar"},
{"time":"2016-12-30 17:20:08","type":"aa"},
{"time":"2016-03-11 17:31:46","type":"aa"},
{"time":"2016-05-04 14:08:25","type":"ar"},
{"time":"2016-11-29 05:21:02","type":"ar"},
{"time":"2016-03-08 05:46:01","type":"ar"},
]
array = array.map(i => i.type);
// ["ae", "ar", ...]
array = array.map(i => {return { type: i.type }})
// [ {"type": "ae"}, {"type": "ar"}, ... ]

How to extract non-existing props as null or empty String from a JSON array by using JSON path

I have Json like this below
{
'orderList': [
{
'productId': 'AAA',
},
{
'productId': 'BBB',
}
]
}
The JSON String is valid when all items in orderList have non-empty productId prop, I'm tring to using JsonPath to validate, now I have a problem. $.orderList[*]['productId'] can extract all productIds from orderList as a String array like ['AAA', 'BBB'], that allows me to check it's items are all non-empty, but when productId prop is not exists like below
{
'orderList': [
{
'productId': 'AAA',
},
{
}
]
}
The JSON path
$.orderList[*]['productId']
gets an array as ['AAA'] ( not ['AAA', null] or ['AAA', ''] ), so it can pass my check functions. How to do this right?
That is how jsonpath works. If it did not find that property in the object it ignores that in the result array instead of adding a null or undefined values. So, if you want to get the null in the result array then you need to add that property with null as value.
{
"orderList": [
{
"productId": "AAA"
},
{
"productId": null
}
]
}
Will result to
[
"AAA",
null
]

Postman test - how to search an array for a list of values without defining index

I'm trying to create a Postman test, and I have a response which includes an array that returns 50+ values I need to verify, and so far I've been writing a test line item for each.
var searchJSON = JSON.parse(responseBody);
tests["check_group_name1"] = /name1/.test(searchJSON.Entity.Group[0].Name);
tests["check_group_name2"] = /name2/.test(searchJSON.Entity.Group[1].Name);
tests["check_group_name3"] = /name3/.test(searchJSON.Entity.Group[2].Name);
Problem is, if/when this API changes, it will be a nightmare to maintain and update each index value.
Is there a way to use a loop to verify 50+ values, without depending on each index position in the array? Is it even possible?
This is what a sample response looks like:
{
"Entity":{
"Group": [
{
"Name": "name1",
},
{
"Name: "name2",
},
{
"Name: "name3",
},
],
}
}
Thanks in advance.
Let's say you start with the array
[
{'Name': 'name1'},
{'Name': 'name2'},
{'Name': 'name3'}
]
You can pull all the values out into a list like this
string json = #"
[
{'Name': 'name1'},
{'Name': 'name2'},
{'Name': 'name3'}
]";
JArray arr = JArray.Parse(json);
List<string> names = arr.Select(token => token.SelectToken("Name").Value<string>()).ToList();
Then you can iterate through the values and check them with whatever test logic you like.

Parse json array into Python list

I have created my own json data with an array I would like to parse into a python list. However, I've been having trouble doing so.
How can I extract my json array into a python list?
json data:
[
{
"ip": "192.168.241.109",
"cameras":
{
"front": "nf091",
"inside": "nf067",
"right": "004317",
"rear": "000189",
"left": "nf084"
}
},
{
"ip": "192.168.241.110",
"cameras":
{
"front": "nf091",
"inside": "nf067",
"right": "004317",
"rear": "000189",
"left": "nf084"
}
}
]
My json data is valid, so I don't know why I'm having trouble with the below code:
system_json = open(json_file)
json_obj = json.load(system_json)
camera_details = [[i['front'], i['rear'], i['left'], i['right'], i['inside']] for i in json_obj['cameras']]
The above code snippet does not work as it yields the list indices must be integers, not str error.
What am I doing wrong, and how can I properly parse my json array into a python list?
The issue is that your JSON "object" is a list, but then you try to index into it with a string (json_obj['cameras']).
What you have is a JSON array, each element of which is a dictionary containing (among other things) a key called "cameras". I believe this code does what you want:
import json
text = """[{"ip": "192.168.241.109", "cameras": {"front": "nf091", "inside": "nf067", "right": "004317", "rear": "000189", "left": "nf084"}}, {"ip": "192.168.241.110", "cameras": {"front": "nf091", "inside": "nf067", "right": "004317", "rear": "000189", "left": "nf084"}}]"""
json_array = json.loads(text)
camera_details = [[i['cameras']['front'], i['cameras']['rear'], i['cameras']['left'], i['cameras']['right'], i['cameras']['inside']] for i in json_array]
print(camera_details)
# Output:
# [['nf091', '000189', 'nf084', '004317', 'nf067'], ['nf091', '000189', 'nf084', '004317', 'nf067']]
EDIT
Possibly clearer/easier?
camera_details = [
[
cameras["front"],
cameras["rear"],
cameras["left"],
cameras["right"],
cameras["inside"],
]
for cameras in [item["cameras"] for item in json_array]
]

Json object pushed into javascript array

I have this javascript array:
var data = ([
['Price', 'Time'],
['1.10661', 1467923348, ],
['1.10675', 1467923349, ],
['1.10681', 1467923350, ],
['1.10690', 1467923351, ],
]);
Im sending a request to my server using ajax and Im reciving as response this json objects:
[{"PRICE":"1.10662","TIME":"1467923352"},{"PRICE":"1.10663","TIME":"1467923353"}]
What im trying to do is to parse the response and push the objects inside of my array so the final result should be this:
var data = ([
['Price', 'Time'],
['1.10661', 1467923348, ],
['1.10675', 1467923349, ],
['1.10681', 1467923350, ],
['1.10690', 1467923351, ],
['1.10662', 1467923352, ],
['1.10663', 1467923353, ],
]);
Please keep in mind the number of objets I receive as response changes in every request. In the example Im giving is received just two objects but this changes all the time.
Thank you in advance
I guess you can break your problem down into 2 parts. First think about how you can make the response data to look the same as data, in terms of data format. Then merge the 2 arrays into one.
Here is my approach. First use the map api to go through every object in resp and extract the property value of PRICE and TIME and store it into a new array. This should leave you with an array of array with position 0 as price and position 1 as time.
[ [ '1.10662', '1467923352' ], [ '1.10663', '1467923353' ] ]
Then use the concat api to combine the 2 arrays into one.
Example:
var data = ([
['Price', 'Time'],
['1.10661', 1467923348, ],
['1.10675', 1467923349, ],
['1.10681', 1467923350, ],
['1.10690', 1467923351, ],
]);
var resp = [{"PRICE":"1.10662","TIME":"1467923352"},{"PRICE":"1.10663","TIME":"1467923353"}];
data = data.concat(
resp.map(entity => { return [ entity.PRICE, entity.TIME ]; })
);
Output:
[ [ 'Price', 'Time' ],
[ '1.10661', 1467923348 ],
[ '1.10675', 1467923349 ],
[ '1.10681', 1467923350 ],
[ '1.10690', 1467923351 ],
[ '1.10662', '1467923352' ],
[ '1.10663', '1467923353' ] ]
You might want to read more about the map and concat api here;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
Thank you very much for the answers! I found the solution, let me share it with you.
(The example is a bit different but the idea is exactly the same)
<html>
<body>
<script>
var myArray = [{"ASK":"1.10662","TIME":"1467923348"},{"ASK":"1.10663","TIME":"1467923346"},{"ASK":"1.10661","TIME":"1467923340"},{"ASK":"1.10663","TIME":"1467923346"},
{"ASK":"1.10661","TIME":"1467923340"}];
var data = ([
['PRICE', 'TIME'],
]);
var actualrows = data.length
var finalrows = data.length + myArray.length;
for( var i=actualrows; i<finalrows; i++ ) {
data.push( [] );
}
while(actualrows < finalrows){
var column = 0;
while(column <2){
data[actualrows][0] = myArray[actualrows-1]['ASK'];
data[actualrows][1] = myArray[actualrows-1]['TIME'];;
column++;
}
actualrows++ ;
}
</script>
</body>
</html>

Resources