Re: Parsing a single-level JSON file

2016-11-18 Thread mike . reider


the end result file looks like this

cat fields.json

{"myField1": {"id": "customfield_10600"}, "myField2": {"id": 
"customfield_11334"}, "myField3": {"id": "customfield_993434"}, etc etc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Parsing a single-level JSON file

2016-11-18 Thread mike . reider
On Friday, November 18, 2016 at 1:23:18 PM UTC-5, mike@gmail.com wrote:
> hi all, 
> 
> Im reading in a JSON file that looks like this
> 
> 
> [  
>{  
>   "name":"myField1",
>   "searchable":true,
>   "navigable":true,
>   "custom":true,
>   "clauseNames":[  
>  "cf[10190]",
>  "Log Details"
>   ],
>   "orderable":true,
>   "id":"customfield_10190",
>   "schema":{  
>  "customId":10190,
>  "type":"string",
>  "custom":"com.atlassian.jira.plugin.system.customfieldtypes:textarea"
>   }
>},
>{  
>   "name":"myField2",
>   "searchable":true,
>   "navigable":true,
>   "custom":true,
>   "clauseNames":[  
>  "cf[10072]",
>  "Sellside Onboarding Checklist"
>   ],
>   "orderable":true,
>   "id":"customfield_10072",
>   "schema":{  
>  "items":"option",
>  "customId":10072,
>  "type":"array",
>  
> "custom":"com.atlassian.jira.plugin.system.customfieldtypes:multicheckboxes"
>   }
>}]
> 
> 
> Lets say I want to get the ID # of MyField1, how can I parse this with json 
> lib? Theyre all on the same level, not sure how to target it to go to 
> MyField1 and get "id" value. 
> 
> Thanks



thanks everyone for the feedback, i got it to work like this using multi-dim 
dictionary

# get JSON to parse
url = "https://"+jira_server+"/rest/api/2/field;
req = requests.get(url,auth=(jira_user,jira_pw), verify=False)
jsonfile = req.json()

# save as key,val pair file
cf = {}
for item in jsonfile:
name = item.get("name")
fid =  item.get("id")
cf[name] = { 'id' : fid }
 
with open(base_dir+'/fields.json','w') as f:
json.dump(cf,f)

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Parsing a single-level JSON file

2016-11-18 Thread Peter Otten
mike.rei...@gmail.com wrote:

> hi all,
> 
> Im reading in a JSON file that looks like this
> 
> 
> [  
>{  
>   "name":"myField1",
>   "searchable":true,
>   "navigable":true,
>   "custom":true,
>   "clauseNames":[
>  "cf[10190]",
>  "Log Details"
>   ],
>   "orderable":true,
>   "id":"customfield_10190",
>   "schema":{
>  "customId":10190,
>  "type":"string",
>  
"custom":"com.atlassian.jira.plugin.system.customfieldtypes:textarea"
>   }
>},
>{  
>   "name":"myField2",
>   "searchable":true,
>   "navigable":true,
>   "custom":true,
>   "clauseNames":[
>  "cf[10072]",
>  "Sellside Onboarding Checklist"
>   ],
>   "orderable":true,
>   "id":"customfield_10072",
>   "schema":{
>  "items":"option",
>  "customId":10072,
>  "type":"array",
>  
"custom":"com.atlassian.jira.plugin.system.customfieldtypes:multicheckboxes"
>   }
>}]
> 
> 
> Lets say I want to get the ID # of MyField1, how can I parse this with
> json lib? Theyre all on the same level, not sure how to target it to go to
> MyField1 and get "id" value.

>>> import json
>>> data = json.loads(s)
>>> [wanted] = [d["id"] for d in data if d["name"] == "myField1"]
>>> wanted
'customfield_10190'

If you need to do this often for the same data you can speed up the process 
with a lookup table:

>>> lookup = {d["name"]: d["id"] for d in data}
>>> lookup["myField1"]
'customfield_10190'
>>> lookup["myField2"]
'customfield_10072'

I'm assuming that the names are unique.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Parsing a single-level JSON file

2016-11-18 Thread MRAB

On 2016-11-18 18:23, mike.rei...@gmail.com wrote:

hi all,

Im reading in a JSON file that looks like this


[
   {
  "name":"myField1",
  "searchable":true,
  "navigable":true,
  "custom":true,
  "clauseNames":[
 "cf[10190]",
 "Log Details"
  ],
  "orderable":true,
  "id":"customfield_10190",
  "schema":{
 "customId":10190,
 "type":"string",
 "custom":"com.atlassian.jira.plugin.system.customfieldtypes:textarea"
  }
   },
   {
  "name":"myField2",
  "searchable":true,
  "navigable":true,
  "custom":true,
  "clauseNames":[
 "cf[10072]",
 "Sellside Onboarding Checklist"
  ],
  "orderable":true,
  "id":"customfield_10072",
  "schema":{
 "items":"option",
 "customId":10072,
 "type":"array",
 
"custom":"com.atlassian.jira.plugin.system.customfieldtypes:multicheckboxes"
  }
   }]


Lets say I want to get the ID # of MyField1, how can I parse this with json lib? Theyre 
all on the same level, not sure how to target it to go to MyField1 and get "id" 
value.

Thanks


Parsing it is easy:

with open(json_path) as json_file:
data_list = json.load(json_file)


If it's a one-off lookup, just do a linear search:

for entry in data_list:
if entry['name'] == 'myField1':
print('ID is {}'.format(entry['id']))
break
else:
print('Not found!')


If you're going to do multiple lookups, turn it into a dict, using the 
'name' field as the key:


data_dict = {entry['name']: entry for entry in data_list}
if 'myField1' in data_dict:
print('ID is {}'.format(data_dict['myField1']['id']))
else:
print('Not found!')

--
https://mail.python.org/mailman/listinfo/python-list


Re: Parsing a single-level JSON file

2016-11-18 Thread John Gordon
In  John Gordon  writes:

> In  
> mike.rei...@gmail.com writes:

> with open("json.dat", "r") as fp:
> data = json.load(fp)
> for item in data:
> if item['name'] == 'myField2':

Oops, that should be 'myField1' of course.

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Parsing a single-level JSON file

2016-11-18 Thread John Gordon
In  
mike.rei...@gmail.com writes:

> Im reading in a JSON file that looks like this
> ... snip ...
> Lets say I want to get the ID # of MyField1, how can I parse this with
> json lib? Theyre all on the same level, not sure how to target it to go
> to MyField1 and get "id" value. 

That data looks like a list of dictionaries:

import json

with open("json.dat", "r") as fp:
data = json.load(fp)
for item in data:
if item['name'] == 'myField2':
print item['id']

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

-- 
https://mail.python.org/mailman/listinfo/python-list


Parsing a single-level JSON file

2016-11-18 Thread mike . reider
hi all, 

Im reading in a JSON file that looks like this


[  
   {  
  "name":"myField1",
  "searchable":true,
  "navigable":true,
  "custom":true,
  "clauseNames":[  
 "cf[10190]",
 "Log Details"
  ],
  "orderable":true,
  "id":"customfield_10190",
  "schema":{  
 "customId":10190,
 "type":"string",
 "custom":"com.atlassian.jira.plugin.system.customfieldtypes:textarea"
  }
   },
   {  
  "name":"myField2",
  "searchable":true,
  "navigable":true,
  "custom":true,
  "clauseNames":[  
 "cf[10072]",
 "Sellside Onboarding Checklist"
  ],
  "orderable":true,
  "id":"customfield_10072",
  "schema":{  
 "items":"option",
 "customId":10072,
 "type":"array",
 
"custom":"com.atlassian.jira.plugin.system.customfieldtypes:multicheckboxes"
  }
   }]


Lets say I want to get the ID # of MyField1, how can I parse this with json 
lib? Theyre all on the same level, not sure how to target it to go to MyField1 
and get "id" value. 

Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list