how to display/differentiate data from json response - json

I have following dynamic response in JSON, so I have one list object and I want to display and differentiate data using form_id. please tell me how to achieve this...
//list object
formsSummery: any = [];
this.formsSummery = (data[0] as any).data_points;
[ {
"job" : 10,
"data_points" : [ {
"user_id" : 11,
"ingest_time" : "2017-09-26T13:17:33.489Z",
"form_id" : 2,
"form_revision" : 1,
"device_id" : 0,
"field1" : "2.2",
"field2" : "two",
"field3" : "2",
"field4" : "1"
}, {
"user_id" : 11,
"ingest_time" : "2017-09-26T13:03:56.757Z",
"form_id" : 7,
"form_revision" : 1,
"device_id" : 0,
"field1" : "thirteen",
"field2" : "fourteen",
"field3" : "fifteen",
"field4" : "sixteen"
}, {
"user_id" : 11,
"ingest_time" : "2017-09-27T11:58:59.735Z",
"form_id" : 7,
"form_revision" : 1,
"device_id" : 0,
"field1" : "17",
"field2" : "TWENTY",
"field3" : "19",
"field4" : "1",
"field5master" : "EIGHTEEN",
"field6" : "TRUE"
}, {
"user_id" : 11,
"ingest_time" : "2017-09-26T07:23:26.468Z",
"form_id" : 7,
"form_revision" : 1,
"device_id" : 0,
"field1" : "five",
"field2" : "seven",
"field3" : "six",
"field4" : "eight"
}, {
"user_id" : 11,
"ingest_time" : "2017-09-26T07:24:14.729Z",
"form_id" : 7,
"form_revision" : 1,
"device_id" : 0,
"field1" : "nine",
"field2" : "eleven",
"field3" : "ten",
"field4" : "twele"
}, {
"user_id" : 11,
"ingest_time" : "2017-09-26T08:29:36.728Z",
"form_id" : 2,
"form_revision" : 1,
"device_id" : 0,
"field1" : "2.1",
"field2" : "one",
"field3" : "1",
"field4" : "true"
}, {
"user_id" : 11,
"ingest_time" : "2017-09-26T07:18:10.401Z",
"form_id" : 7,
"form_revision" : 1,
"device_id" : 0,
"field1" : "one",
"field2" : "three",
"field3" : "two",
"field4" : "four"
}, {
"user_id" : 11,
"ingest_time" : "2017-09-27T11:57:06.188Z",
"form_id" : 2,
"form_revision" : 1,
"device_id" : 0,
"field1" : "2.3",
"field2" : "THREE",
"field3" : "3",
"field4" : "TRUE"
} ]
} ]
I want to display data using form_id.
I want to display data in the table using form_id (all arrays for form_d=7 in one table) like this, and a number of table = number of form ids.

I would suggest first ordering your data_points array, with something like this post. Then use a Set to keep track of the form_id's in your array. As you filter, if the form_id is already in your Set, you'll ignore it.
Example:
let x = new Set();
const y = this.formsSummery[0].data_points.filter(z => {
return !x.has(z.form_id) ? x.add(z.form_id) : null
})
So if !x.has(z.form_id) evaluates to true, x.add(z.form_id) will be returned, which will also evaluate to true - and the desired object will be added to your filtered array.
enter image description here

var ret = 0
function filter(filterId) {
data = formsSummery[0].data_points;
for (i in data) {
if (data[i].form_id == filterId) {
if (ret != 0 && (new Date(data[i].ingest_time).getTime()) >= (new Date(ret.ingest_time).getTime())) {
ret = (data[i]);
}
else {
ret = (data[i])
}
}
}
return ret;
}
var result = filter(2) //your filter id
console.log(result)
not sure about date time

Related

JSON Parsing Issue Swift - Dictionary

I'm trying to retrieve the data from this dictionary and for some reason I cannot seem to acquire it. I'm new to parsing JSON so apologies if this is rough.
let temp = json["list"].arrayValue.map({$0["main"].dictionaryValue})
print(temp[0])
Here I am setting a value equal to the dictionary from the JSON. However, I know I need to add the key's value that I'm searching for. To be clear, I am searching for the "temp" key which in the example is equal to 28.19999...
Here is an example of the JSON:
"list" : [
{
"dt" : 1641524400,
"main" : {
"humidity" : 68,
"sea_level" : 1014,
"temp_max" : 29.260000000000002,
"feels_like" : 28.199999999999999,
"temp_min" : 28.199999999999999,
"grnd_level" : 1004,
"temp" : 28.199999999999999,
"temp_kf" : -0.58999999999999997,
"pressure" : 1014
},{
"dt" : 1641546000,
"main" : {
"pressure" : 1009,
"feels_like" : 20.93,
"temp_max" : 27.100000000000001,
"temp" : 27.100000000000001,
"humidity" : 83,
"grnd_level" : 999,
"sea_level" : 1009,
"temp_min" : 27.100000000000001,
"temp_kf" : 0
},
"sys" : {
"pod" : "n"
},
"pop" : 0.41999999999999998,
"wind" : {
"deg" : 354,
"speed" : 5.4100000000000001,
"gust" : 10.58
},
"visibility" : 6695,
"weather" : [
{
"main" : "Snow",
"id" : 600,
"description" : "light snow",
"icon" : "13n"
}
],
"snow" : {
"3h" : 0.26000000000000001
},
"clouds" : {
"all" : 100
},
"dt_txt" : "2022-01-07 09:00:00"
},
{
"dt" : 1641556800,
"main" : {
"temp_min" : 26.82,
"humidity" : 90,
"pressure" : 1008,
"temp_kf" : 0,
"temp" : 26.82,
"feels_like" : 18.879999999999999,
"sea_level" : 1008,
"temp_max" : 26.82,
"grnd_level" : 998
},
"sys" : {
"pod" : "n"
},
"pop" : 0.97999999999999998,
"wind" : {
"deg" : 310,
"gust" : 14.359999999999999,
"speed" : 7.5199999999999996
}]
Found my answer:
let temp = json["list"].arrayValue.map({$0["main"]["temp"].stringValue})

Jmeter: Extracting JSON response with special/spaces characters

Hello can someone help me extract the value of user parameter which is "testuser1"
I tried to use this JSON Path expression $..data I was able to extract the entire response but unable to extract user parameter. Thanks in advance
{
"data": "{ "took" : 13, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "bushidodb_history_network_eval_ea9656ef-0a9b-474b-8026-2f83e2eb9df1_2021-april-10", "_type" : "network", "_id" : "6e2e58be-0ccf-3fb4-8239-1d4f2af322e21618059082000", "_score" : 1.0, "_source" : { "misMatches" : [ "protocol", "state", "command" ], "instance" : "e3032804-4b6d-3735-ac22-c827950395b4|0.0.0.0|10.179.155.155|53|UDP", "protocol" : "UDP", "localAddress" : "0.0.0.0", "localPort" : "12345", "foreignAddress" : "10.179.155.155", "foreignPort" : "53", "command" : "ping yahoo.com ", "user" : "testuser1", "pid" : "10060", "state" : "OUTGOINGFQ", "rate" : 216.0, "originalLocalAddress" : "192.168.100.229", "exe" : "/bin/ping", "md5" : "f9ad63ce8592af407a7be43b7d5de075", "dir" : "", "agentId" : "abcd-dcd123", "year" : "2021", "month" : "APRIL", "day" : "10", "hour" : "12", "time" : "1618059082000", "isMerged" : false, "timestamp" : "Apr 10, 2021 12:51:22 PM", "metricKey" : "6e2e58be-0ccf-3fb4-8239-1d4f2af322e2", "isCompliant" : false }, "sort" : [ 1618059082000 ] } ] }, "aggregations" : { "count_over_time" : { "buckets" : [ { "key_as_string" : "2021-04-10T08:00:00.000-0400", "key" : 1618056000000, "doc_count" : 1 } ] } }}",
"success": true,
"message": {
"code": "S",
"message": "Get Eval results Count Success"
}
}
Actual Response:
Images
What you posted doesn't look like a valid JSON to me.
If in reality you're getting what's in your image, to wit:
{
"data": "{ \"took\" : 13, \"timed_out\" : false, \"_shards\" : { \"total\" : 5, \"successful\" : 5, \"skipped\" : 0, \"failed\" : 0 }, \"hits\" : { \"total\" : 1, \"max_score\" : 1.0, \"hits\" : [ { \"_index\" : \"bushidodb_history_network_eval_ea9656ef-0a9b-474b-8026-2f83e2eb9df1_2021-april-10\", \"_type\" : \"network\", \"_id\" : \"6e2e58be-0ccf-3fb4-8239-1d4f2af322e21618059082000\", \"_score\" : 1.0, \"_source\" : { \"misMatches\" : [ \"protocol\", \"state\", \"command\" ], \"instance\" : \"e3032804-4b6d-3735-ac22-c827950395b4|0.0.0.0|10.179.155.155|53|UDP\", \"protocol\" : \"UDP\", \"localAddress\" : \"0.0.0.0\", \"localPort\" : \"12345\", \"foreignAddress\" : \"10.179.155.155\", \"foreignPort\" : \"53\", \"command\" : \"pingyahoo.com\", \"user\" : \"testuser1\", \"pid\" : \"10060\", \"state\" : \"OUTGOINGFQ\", \"rate\" : 216.0, \"originalLocalAddress\" : \"192.168.100.229\", \"exe\" : \"/bin/ping\", \"md5\" : \"f9ad63ce8592af407a7be43b7d5de075\", \"dir\" : \"\", \"agentId\" : \"abcd-dcd123\", \"year\" : \"2021\", \"month\" : \"APRIL\", \"day\" : \"10\", \"hour\" : \"12\", \"time\" : \"1618059082000\", \"isMerged\" : false, \"timestamp\" : \"Apr10, 202112: 51: 22PM\", \"metricKey\" : \"6e2e58be-0ccf-3fb4-8239-1d4f2af322e2\", \"isCompliant\" : false }, \"sort\" : [ 1618059082000 ] } ] }, \"aggregations\" : { \"count_over_time\" : { \"buckets\" : [ { \"key_as_string\" : \"2021-04-10T08: 00: 00.000-0400\", \"key\" : 1618056000000, \"doc_count\" : 1 } ] } }}",
"success": true,
"message": {
"code": "S",
"message": "Get Eval results Count Success"
}
}
the easiest way is just using 2 JSON Extractors:
Extract data attribute value into a JMeter Variable from the response
Extract user attribute value into a JMeter variable from ${data} JMeter Variable:
Demo:
If the response looks like exactly you posted you won't be able to use JSON Extractors and will have to treat it as normal text so your choice is limited to Regular Expression Extractor, example regular expression:
"user"\s*:\s*"(\w+)"
Add Regular Expression extractor with the corresponding request and extract it. Use the below expression.
Expression: "user" : "(.*?)"
Ref: https://jmeter.apache.org/usermanual/regular_expressions.html
Regular Expression Extractor Sample

F# - Compare 2 JsonValue'

I have 2 Json values that are similar, but there are some differences.
Json1:
{
"id": "1",
"people" : [
{
"Id" : 1421,
"Name" : "Jackson",
"Age" : 21,
"Status" : "Available"
},
{
"Id" : 5916,
"Name" : "Steven",
"Age" : 22,
"Status" : "Available"
}
],
"totalRecords" : 2
}
Json2:
{
"id": "1",
"people" : [
{
"Id" : 1421,
"Name" : "Jackson",
"Age" : 21,
"Status" : "Available"
},
{
"Id" : 5916,
"Name" : "Steven",
"Age" : 22,
"Status" : "Unavailable"
},
{
"Id" : 1337,
"Name" : "Alice",
"Age" : 19,
"Status" : "Available"
}
],
"totalRecords" : 3
}
I'd like to know if there's a way to compare the two Jsonvalues. At the moment I de-serialize the data into a type and then use the Id's and the status' to see if anythings changed. I then pick out the parts that are different (In the example it'd be Steven and Alice) and add them to a sequence for later.
I'd like to reverse a few of the steps. I'd like too compare the json, find the differences, deserialize them and then add them to the sequence, or add them to the sequence then de-serialize the whole sequence. Either way, same result.
Any ideas?

How to flatten sub-documents in mongo db

My collection has document like this
{
"_id" : ObjectId("587c8d0364b6e32706f7edef"),
"first_name" : "John",
"last_name" : "Doe",
"password" : "aasdjsabb12213b21bbcghc1h2",
"shift" : "A",
"dept" : "Management"
"Requests" : [
{
"weekId" : 1,
"MO" : 1,
"TU" : 2,
"W" : 3,
"TH" : 9,
"FR" : 10,
"SA" : 6,
"SU" : 1
}
]
}
I want to export the result of my query to csv and need fields flattened out like this
{
"_id" : ObjectId("587c8d0364b6e32706f7edef"),
"first_name" : "John",
"last_name" : "Doe",
"password" : "aasdjsabb12213b21bbcghc1h2",
"shift" : "A",
"dept" : "Management"
"weekId" : 1,
"MO" : 1,
"TU" : 2,
"W" : 3,
"TH" : 9,
"FR" : 10,
"SA" : 6,
"SU" : 1
}
I am trying to use aggregate function but to no avail. Can anyone suggest me how to do this?
This is my working code but I don't think this is the right way
db.req.aggregate([{$unwind:'$Requests'},{$project: {first_name:1,last_name:1,dept:1,"WeekId":"$Requests.weekdId","Mon":"$Requests.MO","Tue":"$Requests.TU","Wed":"$Requests.W","Thu":"$Requests.TH","Fri":"$Requests.FR","Sat":"$Requests.SA","Sun":"$Requests.SU"}},{$out:"results"}]);
You can do this with an aggregate query, but its not very pretty:
db.test.aggregate([
{$unwind:"$Requests"},
{$project:
{_id:1,
first_name:1,
last_name:1,
password:1,
shift:1,
dept:1,
weekId:"$Requests.weekId",
MO:"$Requests.MO",
TU:"$Requests.TU",
W:"$Requests.W",
TH:"$Requests.TH",
FR:"$Requests.FR",
SA:"$Requests.SA",
SU:"$Requests.SU"}}])
.pretty()
So basically unwind the Requests array, then project out the document you want to produce. Hope this makes sense.

Reading Key value pairs of a JSON File

My code is as follows: The JSON File is https://www.dropbox.com/s/uwqfqb27blxr1bj/citiesclimate_2014_03_27.json
A small sample:
[ { "_id" : { "$oid" : "5333d7e18828169279d9250d" },
"actions" : null,
"actions_nr" : 0,
"city" : "Adachi City",
"citylink" : "<a href=\"/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198\" target=\"_blank\" >Adachi City</a>",
"commitments" : "338,339",
"commitments_nr" : 1,
"country" : "Japan",
"latitude" : "35.465",
"longitude" : "139.482",
"performance" : "355,356,1090,1091",
"performance_nr" : 4,
"uid" : "198"
},
{ "_id" : { "$oid" : "5333d7e18828169279d92511" },
"test" : [ { "actions" : null,
"actions_nr" : 0,
"city" : "Adachi City",
"citylink" : "<a href=\"/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198\" target=\"_blank\" >Adachi City</a>",
"commitments" : "338,339",
"commitments_nr" : 1,
"country" : "Japan",
"latitude" : "35.465",
"longitude" : "139.482",
"performance" : "355,356,1090,1091",
"performance_nr" : 4,
"uid" : "198"
},
{ "actions" : "3025,3105,3106,3108,3109,3110,3111,3112,3113,3114,3115,3116,3164,3166,3167,3168,3170,3171,3172,3173,3174,3175,3176,3177,3180,3181,3182,3183,3184,3185,3187,3188,3189,3190,3191,3192,3193,3194,3196,3197,3410",
"actions_nr" : 41,
"city" : "Ahmadabad City",
"citylink" : "<a href=\"/data/report/commitments/?tx_datareport_pi1%5Buid%5D=549\" target=\"_blank\" >Ahmadabad City</a>",
"commitments" : "816",
"commitments_nr" : 1,
"country" : "India",
"latitude" : "23.0300",
"longitude" : "72.5800",
"performance" : "900,901",
"performance_nr" : 2,
"uid" : "549"
}
]
}
]
I keep getting string indices must be integers
json_file = source_json
with open(json_file) as json_file:
json_data = json.load(json_file)
for e in json_data: # iterator over a dictionary
#print e
for key, value in e.iteritems():
if key != '_id':
print key, value
#city_climate_data['city'] = value['test.city']
#print city_climate_data['city']
I keep getting string indices must be integers
In JSON a pair must be string : value. It is not possible to have an integer as key.
Edit
I've added a small sample of the JSON to your question. With this JSON, your code will not work because a dictionary does not have iteritems. You can do this:
for e in json_data:
for k, v in e.items():
if k != '_id':
print k, v
That would give this output:
country, Japan
citylink, <a href="/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198" target="_blank" >Adachi City</a>
commitments, 338,339
commitments_nr, 1
longitude, 139.482
performance_nr, 4
actions_nr, 0
latitude, 35.465
uid, 198
actions, None
performance, 355,356,1090,1091
city, Adachi City
test, [{'country': 'Japan', 'commitments': '338,339', 'citylink': '<a href="/data/report/commitments/?tx_datareport_pi1%5Buid%5D=198" target="_blank" >Adachi City</a>', 'commitments_nr': 1, 'longitude': '139.482', 'performance_nr': 4, 'actions_nr': 0, 'latitude': '35.465', 'uid': '198', 'actions': None, 'performance': '355,356,1090,1091', 'city': 'Adachi City'}, {'country': 'India', 'commitments': '816', 'citylink': '<a href="/data/report/commitments/?tx_datareport_pi1%5Buid%5D=549" target="_blank" >Ahmadabad City</a>', 'commitments_nr': 1, 'longitude': '72.5800', 'performance_nr': 2, 'actions_nr': 41, 'latitude': '23.0300', 'uid': '549', 'actions': '3025,3105,3106,3108,3109,3110,3111,3112,3113,3114,3115,3116,3164,3166,3167,3168,3170,3171,3172,3173,3174,3175,3176,3177,3180,3181,3182,3183,3184,3185,3187,3188,3189,3190,3191,3192,3193,3194,3196,3197,3410', 'performance': '900,901', 'city': 'Ahmadabad City'}]
What part of the JSON are you interested in?