There is a lot of information on loading .json files, but I just cannot figure out what the problem is:
I have an external file called LocationHistory.json with various coordinates inside. For reference sake, this is how the data is listed:
{
"data" : {
"items" : [ {
"kind" : "latitude#location",
"timestampMs" : "1374870896803",
"latitude" : 34.9482949,
"longitude" : -85.3245474,
"accuracy" : 2149
}, {
"kind" : "latitude#location",
"timestampMs" : "1374870711762",
"latitude" : 34.9857898,
"longitude" : -85.3526902,
"accuracy" : 2016
}, {
"kind" : "latitude#location",
"timestampMs" : "1374870651752",
"latitude" : 34.9857898,
"longitude" : -85.3526902,
"accuracy" : 2016
}]
}
}
I'm trying to parse this information with:
import json
json_file = open ('LocationHistory.json')
json_string = json_file.read()
json_data = json.loads (json_string)
locations = json_data ["data"]
for location in locations:
print location["timestampMS"], location["latitude"], location["longitude"], location["accuracy"]
Why am I getting the error:
line 10, in
print location["timestampMS"], location["latitude"], location["longitude"], location["accuracy"]
TypeError: string indices must be integers
All the information I can find to parse .json files explains this type of solution that I have. Where am I going wrong?
Thanks in advance, I'm sure it should be a simple mistake...
You want to iterate over data items instead:
locations = json_data["data"]["items"]
for location in locations: # now "locations" is a list of dictionaries
# ...
Related
I am totally new to groovy script and would like some help to solve this out. I have a JSON response I want to manipulate and get desired parameters back by avoiding duplication. The Json response does not have indexes like 0,1,2.. that I can iterate through.
Here is the response that I want to work with:
{
"AuthenticateV2" : {
"displayName" : "Verification of authentication",
"description" : "notification ",
"smsTemplate" : "authentication.v2.0_sms",
"emailHeaderTemplate" : "v2.0_header",
"emailBodyTemplate" : "html",
"parameters" : {
"displayName" : "USER_DISPLAY_NAME",
"actionTokenURL" : "VERIFICATION_LINK",
"customToken" : "VERIFICATION_CODE"
},
"supportedPlans" : [
"connectGo"
]
},
"PasswordRecovery" : {
"displayName" : "Verification of password recovery",
"description" : "notification",
"smsTemplate" : "recovery.v1.0_sms",
"emailHeaderTemplate" : "recovery.v1.0_header",
"emailBodyTemplate" : "recovery.v1.0_body_html",
"parameters" : {
"displayName" : "USER_DISPLAY_NAME",
"actionTokenURL" : "VERIFICATION_LINK",
"customToken" : "VERIFICATION_CODE",
"adminInitiated" : false,
"authnId" : "AUTHENTICATION_IDENTIFIER",
"authnType" : "EMAIL",
"user" : {
"displayName" : "USER_DISPLAY_NAME"
}
},
"supportedPlans" : [
"connectGo"
]
},
"PasswordReset" : {
"displayName" : "password reset",
"description" : "notification",
"smsTemplate" : "recovery.v1.0_sms",
"emailHeaderTemplate" : "recovery.v1.0_header",
"emailBodyTemplate" : "html",
"parameters" : {
"displayName" : "USER_DISPLAY_NAME",
"user" : {
"displayName" : "USER_DISPLAY_NAME"
}
}
The expected output that I want to have:
{
"displayName" : "USER_DISPLAY_NAME",
"actionTokenURL" : "VERIFICATION_LINK",
"customToken" : "VERIFICATION_CODE",
"customToken" : "VERIFICATION_CODE",
"adminInitiated" : false,
"authnId" : "AUTHENTICATION_IDENTIFIER",
"authnType" : "EMAIL"
}
I need to retrieve all fields under parameters tag and also want to avoid duplication
You should first get familiar with parsing and producing JSON in Groovy.
Then, assuming the provided response is a valid JSON (it's not - there are 2 closing curlies (}) missing at the end) to get all the parameters keys merged into one JSON we have to convert the JSON string into a Map object first using JsonSlurper:
def validJsonResponse = '<your valid JSON string>'
Map parsedResponse = new JsonSlurper().parseText(validJsonResponse) as Map
Now, when we have a parsedResponse map we can iterate over all the root items in the response and transform them into the desired form (which is all the unique parameters keys) using Map::collectEntries method:
Map uniqueParameters = parsedResponse.collectEntries { it.value['parameters'] }
Finally, we can convert the uniqueParameters result back into a pretty printed JSON string using JsonOuput:
println JsonOutput.prettyPrint(JsonOutput.toJson(uniqueParameters))
After applying all the above we'll get the output
{
"displayName": "USER_DISPLAY_NAME",
"actionTokenURL": "VERIFICATION_LINK",
"customToken": "VERIFICATION_CODE",
"adminInitiated": false,
"authnId": "AUTHENTICATION_IDENTIFIER",
"authnType": "EMAIL",
"user": {
"displayName": "USER_DISPLAY_NAME"
}
}
If you want to get rid of user entry from the final output just remove it from the resulting uniqueParameters map (uniqueParameters.remove('user')) before converting it back to JSON string.
I am generating random data from the following JSON/AVRO schema:
{
"type" : "record",
"namespace" : "test",
"name" : "metro_data",
"fields": [
{
"name" : "PersonID",
"type" : "int"
},
{
"name" : "TripStartStation",
"type" : {
"type" : "enum",
"name" : "StartStation",
"symbols" : ["WIEHLE_RESTON_EAST", "SPRING_HILL", "GREENSBORO"]
}
},
{
"name" : "TripEndStation",
"type" : {
"type" : "enum",
"name" : "EndStation",
"symbols" : ["WIEHLE_RESTON_EAST", "SPRING_HILL", "GREENSBORO""]
}
}
]
}
The above schema generates this, for example:
[ {
"PersonID" : -1089196095,
"TripStartStation" : "WIEHLE_RESTON_EAST",
"TripEndStation" : "SPRING_HILL"
}
I want to take the PersonID number of the schema, and add it to the Attributes. Eg, the blank in this photo needs to pull the actual PersonID number generated from the flow:
I have tried to use EvaluateJSONPath with the following configuration, and that's how I end up with the empty string set under PersonalID:
Is my next processor UpdateAttribute? Not sure how to pull that content. Thanks!
You are having array of json message(s)(ex: [...]) and You need to split the array of json into individual flowfiles using SplitJson processor with split expression as $.*
Then use EvaluateJsonProcessor to extract PersonID value as a attribute.
Flow:
--> SplitJson --> EvaluateJsonPath--> other processors
For more details refer to this link regards to the same issue.
{
"fruits" : {
"fruit" : [
{
"name" : "apple",
"size" : 1,
"price" : 1
},
{
"name" : "banana",
"size" : 1,
"price" : 2
}
]
},
"sports" : {
"sport" : [
{
"name" : "baseball",
"population" : 9
},
{
"name" : "soccer",
"population" : 11
}
]
}
}
This is my example json file.
I made this file.
If this format is not JSON, please tell me.
I want to get name's value. Using Python.
I can read JSON file and be converting dictionary.
But can't read Specific (tag's maybe) value
import json
#json file reading
with open('C:\Users\sub2\Desktop\example.json') as data_file:
data = json.load(data_file)
#dictionary key
dic_key = []
for i in data:
dic_key.append(i)
#dictionary value
for i in dic_key:
print data[i]
#name tag
for i in dic_key:
for j in data[i]:
print j.get('name')
How do get the name's value.
This should give you all names
import json
#json file reading
with open('out.json') as data_file:
data = json.load(data_file)
#dictionary key
dic_key = []
for i in data:
dic_key.append(i)
#dictionary value
for i in dic_key:
print data[i]
#name tag
for i in dic_key:
for j in data[i]:
for k in data[i][j]:
print k.get('name')
Im using SwiftyJSON, Alamofire_SwiftyJSON and Alamofire libraries. I'm trying to parse my response but it returns nothing("definitions" fails to print).
JSON isn't empty it has got the response.
So, my code looks like the following (nothing fails, it all compiles)
Alamofire.request(request).responseSwiftyJSON { dataResponse in
if let JSON = dataResponse.result.value {
print(JSON)
if let definitions = JSON["results"]["lexicalEntries"]["entries"]["senses"]["definitions"].string {
print(definitions)
print("Hello")
}}
}
My response model looks like (this is not the whole response, that's just what I want to reach :
{
"results" : [
{
"language" : "en",
"id" : "ace",
"type" : "headword",
"lexicalEntries" : [
{
"language" : "en",
"entries" : [
{
"etymologies" : [
"Middle English (denoting the ‘one’ on dice): via Old French from Latin as ‘unity, a unit’"
],
"grammaticalFeatures" : [
{
"type" : "Number",
"text" : "Singular"
}
],
"homographNumber" : "000",
"senses" : [
{
"definitions" : [
"a playing card with a single spot on it, ranked as the highest card in its suit in most card games"
I think that my problem is in, should I add any parentheses or any symbols?
if let definitions = JSON["results"]["lexicalEntries"]["entries"]["senses"]["definitions"].string
You are not indexing the corresponding arrays in your JSON response, to access the definitions array you can simply use
JSON['results'][0]['lexicalEntries'][0]['entries'][0]['senses'][0]['definitions']
My current file looks like this:
{
"result": [
{
"Longitude" : "-075.947332",
"Zipcode" : "21922",
"ZipClass" : "STANDARD",
"County" : "CECIL",
"City" : "ELKTON",
"State" : "MD",
"Latitude" : "+39.593612"
},
{
"Longitude" : "-075.884544",
"Zipcode" : "21930",
"ZipClass" : "PO BOX ONLY",
"County" : "CECIL",
"City" : "GEORGETOWN",
"State" : "MD",
"Latitude" : "+39.366183"
}
]
}
I continuously get the following error:
file should have the following format
{ "results": [ {...}, ... ]}
Any ideas whats going on or how I can import my file.
The link to my file is right here for further understanding: http://gomashup.com/json.php?fds=geo/usa/zipcode/state/MD&jsoncallback=?
Note* (There is no "?" in my JSON file like data in above link.)
Looks like you need to change "result" to "results".
The file you linked to begins with a parenthesis ((), that might be an issue too.