i'm trying to set all the parsed information in json in a dict variable but it returns an empty dict. when i get the array value, everything works completely fine.
here is my code:
let dic = json.arrayValue
for each in dic {
let data = each["data"].dictionaryValue
print (data)
let date = each["date"].stringValue
print (date)
}
parsing date works fine too. and note that my json file is not empty. because when i get the arrayValue everything is fine. here is the output when i print each["data"].arrayValue:
[{
"factoryPrice" : 0,
"size" : 25,
"t5" : 0,
"t3" : 0,
"type" : 1,
"bongahPrice" : 2435,
"sherkat" : "",
"priceConfirmed" : 1,
"id" : 1658,
"factory" : 9,
"exist" : true,
"t1" : 0,
"provice" : 1,
"properties" : {
"طول" : "12 متری",
"info" : "",
"استاندارد" : "A2",
"standard" : "A3",
"رنگ" : "مشکی",
"نوع" : "آجدار"
},
"factoryName" : "نیشابور",
"city" : 306,
"name" : "",
"phoneNumber" : "09338810407",
"createdAt" : "2018-02-16 12:52:50",
"ownerId" : 282,
"shomareSabt" : "",
"t4" : 0,
"profileType" : 0,
"t2" : 0,
"modirName" : "آرزومند",
"bongahName" : "میلگرد تهران",
"updatedAt" : "11:36",
"weight" : 22,
"group" : 57,
"bongahAddress" : "بازاراهن شادباد بوستان بلوک B",
"bongahPhone" : "02166139083"
}]
and this only one of the arrays. i get multiple arrays in response.
so what should i do?
The JSON you have has an array as its top level element. This is clear by the first character: [.
You can't directly get a dictionary value for your JSON because it represents an array. That's the reason trying to get dictionaryValue returns nil.
Related
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
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.
{
"Head1" : {
"roll no" : 2323,
"person name" : "Ankit",
"person mark" : 1124,
"person average" : 92.34657163223787
},
"Head2" : {
"subject1" : 135,
"subject2" : 184,
"subject3" : 200,
"subject4" : 200,
"subject5" : 200,
"subject6" : 180
},
"Head3" : ["{\"mobile\":\"919958254506\",\"profile\":[\"{\\\"profile_TYPE\\\":\\\"PERSONAL\\\",{ \\\"number\\\":\\\"919958254506\\\"} }\",\"{\\\"profile_TYPE\\\":\\\"PERSONAL\\\",{ \\\"number\\\":\\\"919958254506\\\"} }\",\"{\\\"profile_TYPE\\\":\\\"PERSONAL\\\",{ \\\"number\\\":\\\"919958254506\\\"} }\"],\"SMS_DATE\":\"9/15/2015 10:59:59\",\"SMS_DATASOURCE\":\"Hi Ankit. How r u .\"}"]
}
The above is my json i want to display particular value for example head3 value of profile value of profile type and number . how can i parse these value thanks advance..
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?
I am getting the following error in my code:
System.JSONException null Don't know the type of the Apex object to
deserialize at [line:1, column:1] 11 (System Code)
Class.FactorLab.FactorLabWebservices.RetrieveUsersFromFactorLab: line
60, column 1 Class.FactorLab.PullUsers.execute: line 61, column 1
Here is the code in question:
public static List<FactorLabPullUser> RetrieveUsersFromFactorLab(List<String> ids){
HttpRequest req = getHttpRequest(baseUrl + '/ws/sfdc/people/retrieve', 'POST');
req.setBody(JSON.serialize(ids));
req.setHeader('Content-Type', 'application/json');
if(Test.isRunningTest()){
return null;
}
HttpResponse res = sendRequest(req);
// This is the line with the error
List<FactorLabPullUser> flusers = (List<FactorLabPullUser>)JSON.deserialize(res.getBody(), Type.forname('List<FactorLabPullUser>'));
return flusers;
}
I'm sure that it's receiving valid JSON, but I'm not sure the exact JSON it's receiving when it gets this error. It could simply get an empty array:
[]
It could also get something like this:
[ { "SFDCStatus" : "RETRIEVED",
"address" : "123 Fake St.",
"addressLine1" : "Address1",
"addressLine2" : "Address2",
"city" : "San Francisco",
"companyName" : "Big Cheese, Inc.",
"deleted" : false,
"email" : "james.willard#fake.com",
"firstName" : "James",
"hireDate" : "2000-04-15T00:00:00Z",
"id" : 39,
"lastName" : "Willard",
"lastUpdated" : "2011-11-23T05:44:03Z",
"myersBriggs" : "SFDC",
"name" : "James Willard",
"phone" : "415-555-1212",
"position" : "Big Chief",
"regions" : [ { "deleted" : false,
"id" : 445,
"name" : "Mountain"
} ],
"state" : "CA",
"yearsExp" : 20.0,
"zip" : "94131"
},
{ "SFDCStatus" : "RETRIEVED",
"deleted" : false,
"firstName" : "Daniel",
"id" : 40,
"lastName" : "Adams",
"lastUpdated" : "2011-11-23T05:44:03Z",
"name" : "Daniel Adams"
}
]
Any ideas? Someone told me this was a Salesforce.com bug, but it seems like even if that were true, there must be a workaround.
Looks like you may not be casting properly. I haven't played around with the serialize/deserialize native JSON much yet and have preferred to do my own parsing/persisting. But your line causing the error looks suspicious:
List<FactorLabPullUser> flusers = (List<FactorLabPullUser>)JSON.deserialize(res.getBody(), Type.forname('List<FactorLabPullUser>'));
I think it should look like the following:
List<FactorLabPullUser> flusers = (List<FactorLabPullUser>)JSON.deserialize(res.getBody(), List<FactorLabPullUser>.class);
I can't see for sure from your code snippit but if the FactorLabPullUser class is an inner class you will need to fully qualify it for Type.forName() to work.
I would expect to see something like this:
List<FactorLabPullUser> flusers = (List<FactorLabPullUser>)JSON.deserialize(res.getBody(), Type.forname('List<Outerclass.FactorLabPullUser>'));
I think that the bug you mention is to do with the use of the .class method - I have noticed this not always return a valid Type.