Reading Key value pairs of a JSON File - json

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?

Related

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

How to get a JSON node that contains a specific value?

Below is the JSON file I am trying to read nodes from. I am trying to perform contains operation. I do not want to use equals option in my json path like this - $.[?(#.name=="College Graduate - Ford")]
Could you please help me in meeting my requirement?
[
{
"incentiveId" : 123,
"autoApplied" : "false",
"name" : "College Graduate - Ford",
"amount" : 750,
"expirationDate" : "2018-12-31",
"minimumTerm" : null,
"maximumTerm" : null,
"groupAffiliation" : "College/Student",
"previousOwnership" : null
},
{
"incentiveId" : 456,
"autoApplied" : "false",
"name" : "Lease Loyalty - Ford",
"amount" : 500,
"expirationDate" : "2018-07-09",
"groupAffiliation" : null,
"previousOwnership" : "Lease Loyalty"
},
{
"incentiveId" : 789,
"autoApplied" : "false",
"name" : "Customer Cash - Ford",
"amount" : 1000,
"expirationDate" : "2018-06-04",
"groupAffiliation" : null,
"previousOwnership" : null
},
{
"incentiveId" : 222,
"autoApplied" : "false",
"name" : "Military - Ford",
"amount" : 1000,
"expirationDate" : "2018-12-31",
"groupAffiliation" : "Military",
"previousOwnership" : null
}
]
$.[?(/College/.test(#.name))]
Here I am using the test the RegExp test( ) method (which JSONPath evals behind the scenes) . This will perform the contains operation.
you can use filter to get array from array which elements satisfy the condition.
colleagues = json.filter(node => node.name.match("College"))
result:
[
{
"incentiveId": 123,
"autoApplied": "false",
"name": "College Graduate - Ford",
"amount": 750,
"expirationDate": "2018-12-31",
"minimumTerm": null,
"maximumTerm": null,
"groupAffiliation": "College/Student",
"previousOwnership": null
}
]

Getting a dictionary var from parsed json in swift

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.

UISearchBar and JSON Data

This is my first time using a UISearchBar and it is proving to confuse me.
I think my biggest problem here is not understanding how JSON is stored and displayed.
I have my JSON data stored as:
[{
"referralId" : "v-1519014616",
"name" : "Empire State Building",
"storeId" : "",
"hereNow" : {
"count" : 2,
"summary" : "2 people are here",
"groups" : [
{
"count" : 2,
"type" : "others",
"name" : "Other people here",
"items" : [
]
}
]
},
"stats" : {
"tipCount" : 1105,
"checkinsCount" : 185916,
"usersCount" : 129661
},
"venueRatingBlacklisted" : true,
"beenHere" : {
"lastCheckinExpiredAt" : 0
},
"specials" : {
"count" : 0,
"items" : [
]
},
"venuePage" : {
"id" : "64514349"
},
"verified" : true,
"location" : {
"state" : "NY",
"neighborhood" : "Midtown Manhattan, New York, NY",
"crossStreet" : "btwn 33rd & 34th St",
"lat" : 40.748469532965927,
"address" : "350 5th Ave",
"cc" : "US",
"city" : "New York",
"postalCode" : "10118",
"formattedAddress" : [
"350 5th Ave (btwn 33rd & 34th St)",
"New York, NY 10118"
],
"lng" : -73.985513794430119,
"distance" : 17,
"country" : "United States"
},
"hasPerk" : false,
"id" : "43695300f964a5208c291fe3",
"categories" : [
{
... etc
},
I have this stored in an array titled locations and I am trying to filter through it with a search bar.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let name = self.locations[0]["name"].string
filteredLocations = locations.filter { names in
return (name!.lowercased().contains(searchText.lowercased()))
}
tableView.reloadData()
}
Now, obviously, name is only going to search for the item in the array. How would I be able to filter through every location's name key to properly filter?
try this one ... don't take name first from location array...
let filterArr = locations.filter {
return (($0["name"] as! String).lowercased().contains(searchText.lowercased())))
}
use this one to get the search result

how to display/differentiate data from json response

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