How can I write Parsed JSON to a CSV fie? - python

Note: Please see comments of ticked answer for resolution :)
I'm writing some python which accesses an API and the exports the server's response (JSON) to a CSV file. The JSON is nested.
This is the JSON response from the server (this is just a sample of the response):
{
"id":182774,
"website_id":307842,
"engine_provider":"Google",
"engine_name":"United Kingdom",
"keywords":[
{
"id":4464443,
"groups_id":[
44424
],
"name":"SMART E70 Interactive Display",
"positions":{
"2017-03-16":10
}
},
{
"id":4464442,
"groups_id":[
44424
],
"name":"SMART Podium SP518",
"positions":{
"2017-03-16":4
}
},
{
"id":4464441,
"groups_id":[
44424
],
"name":"SMART Board M680",
"positions":{
"2017-03-16":3
}
},
{
"id":4464338,
"groups_id":[
51168
],
"name":"NEC Lamps",
"positions":{
"2017-03-16":4
}
}
]
}
If the JSON is looking wrong, it's probably because I edited it wrong when sampling it for this post.
In Python I try to parse the query response and then write the nested fields to a CSV table like this:
parsedqueryresponse = queryresponse.json()
f = csv.writer(open(csvoutputpath, "wb+"))
f.writerow(["name", "positions", "id"])
for parsedqueryresponse in parsedqueryresponse:
f.writerow([parsedqueryresponse["keywords"]["name"],
parsedqueryresponse["keywords"]["positions"],
parsedqueryresponse["keywords"]["id"]])
When I run the script I get this error:
"line 146, in
f.writerow([parsedqueryresponse["keywords"]["name"],
TypeError: string indices must be integers"
Line 146 is this one (also referenced by the error message):
f.writerow([parsedqueryresponse["keywords"]["name"]
What am I doing wrong here? I tried changing the JSON fields to use ' instead of " but that didn't seem to make things any better...

Please try this,
import csv
import json
parsedqueryresponse = queryresponse.json()
f = csv.writer(open(csvoutputpath, "wb+"))
f.writerow(["name", "positions", "id"])
for entry in parsedqueryresponse["keywords"]:
f.writerow([entry["name"],
entry["positions"],
entry["id"]])
Output:
name,positions,id
SMART E70 Interactive Display,{2017-03-16: 10},4464443
SMART Podium SP518,{2017-03-16: 4},4464442
SMART Board M680,{2017-03-16: 3},4464441
NEC Lamps,{2017-03-16: 4},4464338
Please let me know in terns of any queries.

Related

Converting Excel to JSON using Python

This is my first attempt at something like this, How do I get the desired output?
the desired json is like so
{
"cluster": "ABC,DEF,GHI,SMU,RTR,DIP-OLM"
}
I am following various answers here, and I only have this
import pandas as pd
file = 'my_template.xlsx'
ans = pd.read_excel(file, sheet_name='clusters')
f = ans.to_json(indent=2)
print(f)
which gives
{
"service_name":{
"0":"ABC",
"1":"DEF",
"2":"GHI",
"3":"SMU",
"4":"MDS",
"5":"APS_OLM",
"6":"RTR",
"7":"DIP-OLM",
"8":"LMS-JSX"
}
}
Some help would open the way for me to get a better understanding of this. Thanks in advance. Would xlrd be better suited for this? There are a couple of more sheets that have to be nested inside of this json.
INFO :- The complete json is something like this:
{
"cluster": "ABC,DEF,GHI,SMU,RTR,DIP-OLM",
"version": "07.04.00",
"ABC": {
"microservices": "ABC - HA (07.04.00)",
"maps": {
"pam_bld_nr": "21",
"pam_bld_nr_frontend": "21",
"pam_bld_nr_backend": "21",
"pam_bt_dk_size": "200"
},
"new_block": {
"additional_param": "additional_param_value",
"additional_param": "additional_param_value2"
}
},
Rest of the values need to come from the other 2 sheets in the same file. Just FYI.

Adding json data file to another common json file in Python

First of all I have search the similar issues of mine but none able to answer my question above. I hope you guys could advise me further.
I' running a script to extract data from a list of network equipment and save the value onto json file for example below
json-1 = {
"channel": "scanner",
"action": "create_device",
"table": "U2",
"device":[]
}
data = "device_name","ip_address","lldp_neighbors"
Then a line of code is used to get data of devicename,ipaddress and lldp, return the value, extract it and save it onto the data list above. For example
my[data[0]] = connection.find_prompt().rstrip('>') #to get hostname
my[data[1]] = device['ip'] #to get ip address
my[data[2]] = connection.send_command('show lldp neighbors | display xml')
#to get lldp data in xml format
json1["device"].append(my) #append my data to device
For my[data[2]], lldp neighbors will return data in xml format and convert that xml data onto json format file like below
LLDP NEIGHBORS DETAILS:-
"lldp_neighbors": [
{
"local-port": "xe-3/0/4.0",
"parent-interface": "ae31.0",
"chassis-id": "b0:c6:9a:63:80:40",
"port-info": "xe-0/0/0/0.0",
"system-name": "host.jnpr.net"
}
My questions here is how can i add lldp neighbors detail above (json data) onto temp[data[2]] of json-1 so that the final json file json.dump(json-1, fp) generated will be like below, nested json file
{
"channel": "scanner",
"action": "create_device",
"table": "U2",
"device": [
{
"device_name": "rtr1.wer",
"ip_address": "1.1.1.1",
"lldp_neighbors": [
{
"local-port": "xe-3/0/4.0",
"parent-interface": "ae31.0",
"chassis-id": "b0:c6:9a:63:80:40",
"port-info": "xe-0/0/0/0.0",
"system-name": "host.jnpr.net"
}
]
]
}
I really hope someone could point me to the right path...i'm stuck ...please assist me. Thank you.
Your data should be a dictionary type, now a tuple type
data = "device_name","ip_address","lldp_neighbors"
# change to
data = {"device_name": "","ip_address": "","lldp_neighbors": []}
my[data["device_name"]] = connection.find_prompt().rstrip('>') #to get hostname
my[data["ip_address"]] = device['ip'] #to get ip address
my[data["lldp_neighbors"]] = connection.send_command('show lldp neighbors | display xml')

How to retrieve values from a json file in a particular order?

I am new to python. I am writing a code, where I need to read the json file, and dump some of it's data to a new json file.
Following is my code:
if vmName=='IGW':
with open(APIJSONPath+NwIntfJSONFileName) as f:
data=json.load(f)
for item in data['Interfaces']:
jdata=item
with open(NwIntfJSONPath+vmName+'.json','w') as c:
json.dump(jdata,c,indent=2)
Following is a small part of my json file data from which this code is supposed to retrieve the interface details(Interface name,IPAddress, PrefixLength, DefaultGateway) of eth0 and eth1:
{
"Interfaces": [{
"Name": "eth0",
"IPConfigurations": [{
"IPAddress": "10.0.3.7",
"PrefixLength": 24,
"DefaultGateway": "10.0.3.1",
"IsPrimary": true
}],
"Description0": "connected to cloudsimple network",
"IsPrimary": true
} ,
{
"Name": "eth1",
"IPConfigurations": [{
"IPAddress": "10.0.3.8",
"PrefixLength": 24,
"DefaultGateway": "10.0.3.1",
"IsPrimary": true
}],
"Description1": "connected to internet"
}
]
}
But the data that is getting dumped the new json file is:
{
"Name": "eth1",
"IPConfigurations": [
{
"PrefixLength": 24,
"IsPrimary": true,
"IPAddress": "10.0.3.8",
"DefaultGateway": "10.0.3.1"
}
],
"Description1": "connected to internet"
}
Only eth1 details is getting dumped, not the eth0. The dumped data is also in an unordered manner.
Can someone please help me figure out, where I am going wrong, and how to fix this two issues in my code? Thanks in advance.
If you need all content of data['Interfaces'] in your output json use the below snippet.
if vmName=='IGW':
with open(APIJSONPath+NwIntfJSONFileName) as f:
data=json.load(f)
with open(NwIntfJSONPath+vmName+'.json','w') as c:
json.dump(data['Interfaces'],c,indent=2)
In your example you are looping through data['Interfaces'] and jdata holds the last value of the list. That is why you are only getting the last element in the output json.

Getting TypeError while spliting Json data?

I have A json data like this:
json_data = '{"data":"[{"Date":"3/17/2017","Steam Total":60},{"Date":"3/18/2017","Steam Total":15},{"Date":"3/19/2017","Steam Total":1578},{"Date":"3/20/2017","Steam Total":1604}]", "data_details": "{"data_key":"Steam Total", "given_high":"1500", "given_low":"1000", "running_info": []}"}'
json_input_data = json_data["data"]
json_input_additional_info = json_data["data_details"]
I am getting an error:
TypeError: string indices must be integers, not str
I think there is an error in the json data. Can someone Help me on this?
In you code has some issues.
The code: json_input_data = json_data["data"], the variable json_data is not a Json Object, is a String Object and you try get a string position by string key, for get a Json object from string json use json api: json
You Json string isn't valid, this is a valid version:
{"data":[{"Date":"3/17/2017","Steam Total":60},{"Date":"3/18/2017","Steam Total":15},{"Date":"3/19/2017","Steam Total":1578},{"Date":"3/20/2017","Steam Total":1604}], "data_details": {"data_key":"Steam Total", "given_high":"1500", "given_low":"1000", "running_info": []}}
Now, your code works fine.
Try parsing your json_data to JSON format (with JSON.parse(json_data)). Currently it's type is string - which is exactly what your error says.
As Pongpira Upra pointed out, your json is not well formed and should be something like this.
{
"data":[
{
"Date":"3/17/2017",
"Steam Total":60
},
{
"Date":"3/18/2017",
"Steam Total":15
},
{
"Date":"3/19/2017",
"Steam Total":1578
},
{
"Date":"3/20/2017",
"Steam Total":1604
}
],
"data_details":{
"data_key":"Steam Total",
"given_high":"1500",
"given_low":"1000",
"running_info":[]
}
}
In order to retrieve information you should write
json_data[0]["Date"]
This would print "3/17/2017"
You declare a string called json_data and, well, then it acts like a string. That is what the exception tells you. Like others here tried to say - you do also have an error in your data, but the exception you supplied is due to accessing the string as if it was a dictionary. You need to add a missing call to e.g. json.loads(...).
You were right. Your JSON is indeed wrong.
Can you try using this json?
{
"data":[
{
"Date":"3/17/2017",
"Steam Total":60
},
{
"Date":"3/18/2017",
"Steam Total":15
},
{
"Date":"3/19/2017",
"Steam Total":1578
},
{
"Date":"3/20/2017",
"Steam Total":1604
}
],
"data_details":{
"data_key":"Steam Total",
"given_high":"1500",
"given_low":"1000",
"running_info":[]
}
}

Python Objects and Lists within Dictionary

I'm pretty new to Python, so just working my way through understanding the data sets.
I'm having a little trouble producing the JSON output that is required for the API I am working with.
I am using
import json
json.load(data_file)
Working with Python dictionary and then doing
json.dump(dict, json_data)
My data needs to look like the following when it is output.
{
"event":{
"id":10006,
"event_name":"My Event Name",
},
"sub event":[
],
"attendees":[
{
"id":11201,
"first_name":"Jeff",
"last_name":"Smith",
},
{
"id":10002,
"first_name":"Victoria",
"last_name":"Baker",
},
]
}
I have been able to create the arrays in python and dump to json, but I am having difficulty creating the event "object" in the dictionary. I am using the below:
attendees = ['attendees']
attendeesdict = {}
attendeesdict['first_name'] = "Jeff"
attendees.append(attendeesdict.copy())
Can anyone help me add the "event" object properly?
In general, going from JSON to dictionary is almost no work because the two are very similar, if not identical:
attendees = [
{
"first_name": "Jeff"
# Add other fields which you need here
},
{
"first_name": "Victoria"
}
]
In this instance, attendees is a list of dictionaries. For the event:
event = {
"id": 10006,
"event_name": "My Event Name"
}
Putting it all together:
data = {
"event": event,
"sub event": [],
"attendees": attendees
}
Now, you can convert it to a JSON object, ready to send to your API:
json_object = json.dumps(data)
Assuming you have built all the values elsewhere and now you're just putting them together:
result = {'event':event_dict, 'sub event':subevent_list, 'attendees':attendees_list}
If you want just to statically create a nested dict, you can use a single literal. If you paste the JSON above into python code, you would get a valid dict literal.
Construct your dicts and add like below
{
"event":"add your dict"
"sub event":["add your dict"],
"attendees":["add your dict"]
}

Categories

Resources