JSON Parsing Issue Swift - Dictionary - json

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})

Related

file_get_contents('php://input'); is not working

$json = file_get_contents('php://input');
is not returning any result
sample JSON I was supposed to get from my payment gateway is here (sorry I couldn't post sample code here please see comment)
https://uat-paco.s3.ap-southeast-1.amazonaws.com/HTML-based-docs/CorePaymentAPI/2022-04-25-Et01x9/APIs/Payment.html#_inline_response_202_data
{
"version" : "1.0.0",
"apiResponse" : {
"responseMessageID" : "fadaba8f-b02c-4ff4-a8f0-2e5ce9873185",
"responseToRequestMessageID" : "e952828c-6d19-4a08-92e4-1bc47e1945a2",
"responseCode" : "PC-B050000",
"responseDescription" : "Success",
"responseDateTime" : "2016-08-29T09:12:34.001+0000",
"responseTime" : 2400,
"marketingDescription" : "Payment is success. Thank you for purchase."
},
"data" : {
"paymentIncompleteResult" : {
"officeGroupId" : "string",
"officeId" : "string",
"transactionDateTime" : "2016-08-29T09:12:34.001+0000",
"orderNo" : "684105191128073630",
"productDescription" : "URRT55",
"invoiceNo2C2P" : "FPX054658",
"pspReferenceNo" : "W32UPVAPCP260543864232",
"controllerInternalID" : "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
"paymentStatusInfo" : {
"paymentStatus" : "A",
"paymentStep" : "PA",
"lastUpdatedDTTM" : "2016-08-29T09:12:35.001+0000"
},
"paymentType" : "CC-VI",
"paymentExpiryDateTime" : "2016-08-30T23:59:59.999+0000",
"channelCode" : "WEBPAY",
"agentCode" : "FPX_SBIA",
"mcpFlag" : "N",
"mcpDetails" : {
"mcpType" : "DCC",
"fxRate" : {
"fxRate" : 32.9,
"markUp" : 1.2,
"convertedAmount" : {
"amountText" : "000000006135",
"currencyCode" : "USD",
"decimalPlaces" : 2,
"amount" : 61.349694
},
"fxProviderId" : "FXP0001",
"fxDate" : "2016-08-29T09:12:34.001+0000"
}
},
"merchantIdForMCP" : "4572325756",
"transactionAmount" : {
"amountText" : "000000999950",
"currencyCode" : "THB",
"decimalPlaces" : 2,
"amount" : 9999.5
},
"settlementAmount" : {
"amountText" : "000000999950",
"currencyCode" : "THB",
"decimalPlaces" : 2,
"amount" : 9999.5
},
"customFieldList" : [ {
"fieldName" : "ExternalID5",
"fieldValue" : "AMADEUS"
} ],
"aresACSChallenge" : {
"transStatus" : "C",
"acsURL" : "https://uatemvco.2c2p.com/challenge",
"creq" : "Base64url encoded form data"
},
"authentication3DSVersion" : "2.1.0",
"availablePaymentTypes" : [ "CC-VI", "CC-CA" ],
"untokenizedStoredCardList" : [ {
"storedCardUniqueID" : "string",
"isExpired" : true,
"cardNumber" : "401200XXXXXX3335",
"cardExpiryMMYY" : "1124",
"cvvCode" : "123",
"payerName" : "Attasit Txxxxxx",
"issuerBankCountry" : "TH",
"issuerBankName" : "JPMORGAN CHASE BANK N.A."
} ]
},
"paymentPage" : {
"paymentPageURL" : "https://demo-paco.2c2p.com/payment/form",
"validTillDateTime" : "2016-08-29T09:12:34.001+0000"
}
}
}

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

How to reformat specific data from json with jq

I'm new to json and want to extract data (blocks) from a specific json. I've tried to find information on how to do this with jq but so far I cannot seem to get what I want.
My json:
{
"now" : 1589987097.9,
"aircraft" : [
{
"mlat" : [],
"rssi" : -26.2,
"track" : 319,
"speed" : 354,
"messages" : 16,
"seen" : 0.7,
"altitude" : 38000,
"vert_rate" : 0,
"hex" : "44b5b4",
"tisb" : []
},
{
"squawk" : "6220",
"altitude" : 675,
"seen" : 1.1,
"messages" : 7220,
"tisb" : [],
"hex" : "484a95",
"mlat" : [],
"rssi" : -22
},
{
"hex" : "484846",
"tisb" : [],
"messages" : 20,
"speed" : 89,
"seen" : 0.4,
"squawk" : "7000",
"altitude" : 500,
"rssi" : -23.7,
"track" : 185,
"mlat" : []
},
{
"category" : "B1",
"mlat" : [],
"rssi" : -24.3,
"flight" : "ZSGBX ",
"altitude" : 3050,
"squawk" : "7000",
"seen" : 16.8,
"messages" : 37,
"tisb" : [],
"hex" : "00901a"
}
],
"messages" : 35857757
}
I would like to reformat this json to only include 'blocks' that contain specific hex values.
So for example, I want I would like my output to contain 44b5b4 and 00901a:
{
"now" : 1589987097.9,
"aircraft" : [
{
"mlat" : [],
"rssi" : -26.2,
"track" : 319,
"speed" : 354,
"messages" : 16,
"seen" : 0.7,
"altitude" : 38000,
"vert_rate" : 0,
"hex" : "44b5b4",
"tisb" : []
},
{
"category" : "B1",
"mlat" : [],
"rssi" : -24.3,
"flight" : "ZSGBX ",
"altitude" : 3050,
"squawk" : "7000",
"seen" : 16.8,
"messages" : 37,
"tisb" : [],
"hex" : "00901a"
}
],
"messages" : 35857757
}
Can someone tell me how to remove all items not having those 2 hex identifiers but still keep the same json structure?
Thanks a lot!
Do a select() on the array aircraft, matching only the required hex values. The map() function takes input the entire array and the result of the select operation i.e. filtering of objects based on the .hex value is updated back |= to the original array and the rest of the fields are kept intact.
jq '.aircraft |= map(select(.hex == "44b5b4" or .hex == "00901a"))' json
Select blocks whose hex matches one of the specific values and update aircraft to leave only those.
.aircraft |= map(select(.hex | IN("44b5b4", "00901a")))
Online demo

Extract JSON value using Jmeter

I have this JSON:
{
"totalMemory" : 12206567424,
"totalProcessors" : 4,
"version" : "0.4.1",
"agent" : {
"reconnectRetrySec" : 5,
"agentName" : "1001",
"checkRecovery" : false,
"backPressure" : 10000,
"throttler" : 100
},
"logPath" : "/eq/equalum/eqagent-0.4.1.0-SNAPSHOT/logs",
"startTime" : 1494837249902,
"status" : {
"current" : "active",
"currentMessage" : null,
"previous" : "pending",
"previousMessage" : "Recovery:Starting pipelines"
},
"autoStart" : false,
"recovery" : {
"agentName" : "1001",
"partitionInfo" : { },
"topicToInitialCapturePosition" : { }
},
"sources" : [ {
"dataSource" : "oracle",
"name" : "oracle_source",
"captureType" : "directOverApi",
"streams" : [ ],
"idlePollingFreqMs" : 100,
"status" : {
"current" : "active",
"currentMessage" : null,
"previous" : "pending",
"previousMessage" : "Trying to init storage"
},
"host" : "192.168.191.5",
"metricsType" : { },
"bulkSize" : 10000,
"user" : "STACK",
"password" : "********",
"port" : 1521,
"service" : "equalum",
"heartbeatPeriodInMillis" : 1000,
"lagObjective" : 1,
"dataSource" : "oracle"
} ],
"upTime" : "157 min, 0 sec",
"build" : "0-SNAPSHOT",
"target" : {
"targetType" : "equalum",
"agentID" : 1001,
"engineServers" : "192.168.56.100:9000",
"kafkaOptions" : null,
"eventsServers" : "192.168.56.100:9999",
"jaasConfigurationPath" : null,
"securityProtocol" : "PLAINTEXT",
"stateMonitorTopic" : "_state_change",
"targetType" : "equalum",
"status" : {
"current" : "active",
"currentMessage" : null,
"previous" : "pending",
"previousMessage" : "Recovery:Starting pipelines"
},
"serializationFormat" : "avroBinary"
}
}
I trying using Jmeter to extract out the value of agentID, how can I do that using Jmeter, what would be better ? using extractor or json extractor?
what I am trying to do is to extract agentID value in order to use it on another http request sample, but first I have to extract it from this request.
thanks!
I believe using JSON Extractor is the best way to get this agentID value, the relevant JsonPath query will be as simple as $..agentID
Demo:
See the following reference material:
JsonPath - Getting Started - for initial information regarding JsonPath language, functions, operators, etc.
JMeter's JSON Path Extractor Plugin - Advanced Usage Scenarios - for more complex scenarios.

mongoDB limit the number of entries scanned

I'm trying mongoDB and I need translate this following SQL query.
SELECT * FROM infos_cli
WHERE MATCH(denomination) AGAINST('cafe')
WHERE code_postal LIKE '34%'
My full text index definition:
db.infos_cli.createIndex(
{ "code_postal": 1,
"denomination": "text"
},
{default_language: "french"},
{name: "indexSerch"}
)
And my query in mongoDb:
db.infos_cli.find({code_postal : /34/, $text: {$search: "cafe"}})
But it's not working.
Can anyone explain how I've to do ?
In this case please create separate index for postal field and for text search
db.articles.createIndex({ author : 1}) //postal... in your case
db.articles.createIndex({
"denomination" : "text"
}, {
default_language : "french"
}, {
name : "indexSerch"
})
my query
db.getCollection('articles').find({
$text : {
$search : "coffee",
$language : "french"
}
}).explain()
Result shows that there is TEXT phase and IXSCAN which is desired!
result from explain:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "testCode.articles",
"indexFilterSet" : false,
"parsedQuery" : {
"$text" : {
"$search" : "coffee",
"$language" : "french",
"$caseSensitive" : false,
"$diacriticSensitive" : false
}
},
"winningPlan" : {
"stage" : "TEXT",
"indexPrefix" : {},
"indexName" : "denomination_text",
"parsedTextQuery" : {
"terms" : [
"coffe"
],
"negatedTerms" : [],
"phrases" : [],
"negatedPhrases" : []
},
"inputStage" : {
"stage" : "TEXT_MATCH",
"inputStage" : {
"stage" : "TEXT_OR",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"_fts" : "text",
"_ftsx" : 1
},
"indexName" : "denomination_text",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "backward",
"indexBounds" : {}
}
}
}
},
"rejectedPlans" : []
},
"serverInfo" : {
"host" : "gbernas3-lt",
"port" : 27017,
"version" : "3.2.0",
"gitVersion" : "45d947729a0315accb6d4f15a6b06be6d9c19fe7"
},
"ok" : 1
}
any comments welcome!