How to deal with the slash and %2F in Python? - python

Giving the parameters of Interface=['1/1/1', '1/2/3'], tag=11, I need to add the missing interface to json_data[Interface] and json_data[Port] as below. All places except name value used '%2F' as '/'.
Input json:
{
"Interface": {
"1%2F1%2F1": {
"name": "1/1/1",
}
},
"Port": {
"1%2F1%2F1": {
"interfaces": [
"1%2F1%2F1"
],
"name": "1/1/1",
"tag": "10"
}
}
}
Output json should be look like this:
{
"Interface": {
"1%2F1%2F1": {
"name": "1/1/1",
},
"1%2F2%2F3": {
"name": "1/2/3",
}
},
"Port": {
"1%2F1%2F1": {
"interfaces": [
"1%2F1%2F1"
],
"name": "1/1/1",
"tag": "10"
},
"1%2F2%2F3": {
"interfaces": [
"1%2F2%2F3"
],
"name": "1/2/3",
"tag": "11"
}
}
}
I tried with iterate interface list:
for item in interface:
if item is not in json_data["Interface"].keys():
json_data["Interface"][item] = { "name" : item }
But returned in wrong format:
"Interface": {
"2": {
"name": "2"
},
"1": {
"name": "1"
},
"'": {
"name": "'"
},
" ": {
"name": " "
},
",": {
"name": ","
},
"/": {
"name": "/"
},

How to deal with the slash and %2F
Here's a simple way to convert from / to %2F:
'1/1/1'.replace('/', '%2F')
# Results: '1%2F1%2F1'
I'm not sure why you need it in that format, but if it's related to passing it through a URL and you need to escape other characters, you could use:
import urllib
urllib.parse.quote('1/1/1', safe='')
# Results: '1%2F1%2F1'
Your output
It looks like you're accidentally iterating over a string of letters and not a list of interfaces. How are you assigning the interface variable?
Your check for existing interfaces
Your condition if item is not in json_data["Interface"].keys() will not work if item is directly from your input in the format 1/1/1 because all of the keys in json_data["Interface"] use the escaped format.

Related

python parsing strange JSON data

How should I parse (with Python3) data in this "unusual" format?
As you can see inside the "variables" dictionary the data that is in capitals has no label, it is provided as a literal. Therefore when I loop over the entries inside "variables" all I get is the strings in capitals, nothing else. I need, obviously, to get the capitals plus the value inside it.
{
"variables": {
"ABSENCE_OSL_PROD": {
"value": "REZWWnBTejN5Ng=="
},
"ACTION_OSL_INT": {
"value": "S0RXSVNTbmFhNw=="
},
"ACTION_OSL_PROD": {
"value": "RUJCaDJGnmFnUg=="
},
"API_STORE_OSL_INT": {
"value": "U3lxaVhogWtIcg=="
}
},
"id": 4,
"type": "Vsts"
}
To load the variables inside variables in the local variable space:
data = {
"variables": {
"ABSENCE_OSL_PROD": {
"value": "REZWWnBTejN5Ng=="
},
"ACTION_OSL_INT": {
"value": "S0RXSVNTbmFhNw=="
},
"ACTION_OSL_PROD": {
"value": "RUJCaDJGnmFnUg=="
},
"API_STORE_OSL_INT": {
"value": "U3lxaVhogWtIcg=="
}
},
"id": 4,
"type": "Vsts"
}
for variable_name, variable_content in data['variables'].items():
locals()[variable_name] = variable_content['value']
print(ABSENCE_OSL_PROD)
# prints "REZWWnBTejN5Ng=="
With dict comprehension you can get a time efficient manner:
ugly = {
"variables": {
"ABSENCE_OSL_PROD": {
"value": "REZWWnBTejN5Ng=="
},
"ACTION_OSL_INT": {
"value": "S0RXSVNTbmFhNw=="
},
"ACTION_OSL_PROD": {
"value": "RUJCaDJGnmFnUg=="
},
"API_STORE_OSL_INT": {
"value": "U3lxaVhogWtIcg=="
}
},
"id": 4,
"type": "Vsts"
}
proper = {elt: ugly["variables"][elt]["value"] for elt in ugly["variables"]}
print(proper)
returns
{'ABSENCE_OSL_PROD': 'REZWWnBTejN5Ng==', 'ACTION_OSL_INT': 'S0RXSVNTbmFhNw==', 'ACTION_OSL_PROD': 'RUJCaDJGnmFnUg==', 'API_STORE_OSL_INT': 'U3lxaVhogWtIcg=='}```

How can I fix this regex to match an object in Json and replace it as a list of Object

I have tried the following but i am failing to match the object in Json
:\s*(\{[^\"]*\})
I want to know the way to replace the object type in Json as list of object.
Here is the sample of Json:
{
"resourceType": "ChargeItem",
"id": "example",
"text": {
"status": "generated",
"session": "Done"
},
"identifier": [
{
"system": "http://myHospital.org/ChargeItems",
"value": "654321"
}
],
"definitionUri": [
"http://www.kbv.de/tools/ebm/html/01520_2904360860826220813632.html"
],
"status": "billable",
"code": {
"coding": [
{
"code": "01510",
"display": "Zusatzpauschale für Beobachtung nach diagnostischer Koronarangiografie"
}
]
}
}
I need i want to convert to this form:
{
"resourceType": "ChargeItem",
"id": "example",
"text": [{
"status": "generated",
"session": "Done"
}],
"identifier": [
{
"system": "http://myHospital.org/ChargeItems",
"value": "654321"
}
],
"definitionUri": [
"http://www.kbv.de/tools/ebm/html/01520_2904360860826220813632.html"
],
"status": "billable",
"code": [{
"coding": [
{
"code": "01510",
"display": "Zusatzpauschale für Beobachtung nach diagnostischer Koronarangiografie"
}
]
}]
}
This appears to be a few simple transformations:
First, change
"text": {
to
"text": [{
Second, change
},
"identifier": [
to
}],
"identifier": [
Third, change
"code": {
to
"code": [{
And finally, change
}
}
<EOF>
to
}]
}
<EOF>
However, it might not be as straightforward as it appears, i.e. what if the identifer section isn't always present, or doesn't immediately follow the text section?
Regular expressions are a poor choice for doing this work. It would be much better to read the json file into a native Python data structure, apply your desired changes, then save the json back to the file.
Solution using a multiline regexp search
>>> import re
>>> blocks = re.compile(r'(?ms)(.*)("text": )([{][^{}]+[}])(,.*"status": "billable"[^"]+)("code": )([{][^"]+"coding":[^]]+\]\s+\})')
>>> m = blocks.search(s)
>>> result = ""
>>> for i in range(1,len(m.groups()) + 1):
... if i not in (3,6):
... result += m.group(i)
... else:
... result += "[" + m.group(i) + "]"
...
>>> result += "\n}"

How do i make this JSON structure work as intended?

I have some data from a project where the variables can change from motorcycle and car. I need to get the name out of them and that value is inside the variable.
This is not the data i will be using but it has the same structure, the "official" data is some persional information so i changed it to some random values. I can not change the structure of the JSON data since this is the way the serveradmins decided to structure it for some reason.
This is my python code:
import json
with open('exampleData.json') as j:
data = json.load(j)
name = 0
Vehicle = 0
for x in data:
print(data['persons'][x]['name'])
for i in data['persons'][x]['things']["Vehicles"]:
print(data['persons'][x]['things']['Vehicles'][i]['type']['name'])
print("\n")
This is my Json data i extracted from the file "ExampleData.json"(sorry for long but it is kinda complex and necessary to understand the problem):
{
"total": 2,
"persons": [
{
"name": "Sven Svensson",
"things": {
"House": "apartment",
"Vehicles": [
{
"id": "46",
"type": {
"name": "Kawasaki ER6N",
"type": "motorcyle"
},
"Motorcycle": {
"plate": "aaa111",
"fields": {
"brand": "Kawasaki",
"status": "in shop"
}
}
},
{
"id": "44",
"type": {
"name": "BMW m3",
"type": "Car"
},
"Car": {
"plate": "bbb222",
"fields": {
"brand": "BMW",
"status": "in garage"
}
}
}
]
}
},
{
"name": "Eric Vivian Matthews",
"things": {
"House": "House",
"Vehicles": [
{
"id": "44",
"type": {
"name": "Volvo XC90",
"type": "Car"
},
"Car": {
"plate": "bbb222",
"fields": {
"brand": "Volvo",
"status": "in garage"
}
}
}
]
}
}
]
}
I want it to print out something like this :
Sven Svensson
Bmw M3
Kawasaki ER6n
Eric Vivian Matthews
Volvo XC90
but i get this error:
print(data['persons'][x]['name'])
TypeError: list indices must be integers or slices, not str
Process finished with exit code 1
What you need is
for person in data["persons"]:
for vehicle in person["things"]["vehicles"]:
print(vehicle["type"]["name"])
type = vehicle["type"]["type"]
print(vehicle[type]["plate"])
Python for loop does not return the key but rather an object here:
for x in data:
Referencing an object as key
print(data['persons'][x]['name'])
Is causing the error
What you need is to use the returning json object and iterate over them like so:
for x in data['persons']:
print(x['name'])
for vehicle in x['things']['Vehicles']:
print(vehicle['type']['name'])
print('\n')

Can't format the JSON data

Below is the sample JSON data that i obtained from API call. But when i try to format the JSON data, i couldn't able to do it.
{
"jsonrpc": "2.0",
"result": [
{
"status": "3",
"name": "Windows",
"triggers": [
{ "triggerid": "11234" },
{ "triggerid": "5465" },
{ "triggerid": "56465" },
{ "triggerid": "56465" },
{ "triggerid": "54364" },
{ "triggerid": "564654" },
{ "triggerid": "564365" },
{ "triggerid": "5434" },
{ "triggerid": "54354" },
{ "triggerid": "5454" },
{ "triggerid": "5645" },
{ "triggerid": "543654" },
{ "triggerid": "546543" }
],
"items": [
{ "name": "connection check" },
{ "name": "Version of apache running" },
{ "name": "Average IOPS " },
{ "name": "Average wrtie speed" },
{ "name": "file in speed" }
],
"templateid": "456434",
"discoveries": []
},
{
"status": "3",
"name": "linux_server",
"triggers": [
{ "triggerid": "11234" },
{ "triggerid": "5465" },
{ "triggerid": "56465" },
{ "triggerid": "56465" },
{ "triggerid": "54364" },
{ "triggerid": "564654" },
{ "triggerid": "564365" },
{ "triggerid": "5434" },
{ "triggerid": "54354" },
{ "triggerid": "5454" },
{ "triggerid": "5645" },
{ "triggerid": "543654" },
{ "triggerid": "123543" }
],
"items": [
{ "name": "connection check" },
{ "name": "Version of docker running" },
{ "name": "Average IOPS " },
{ "name": "Average wrtie speed" },
{ "name": "file in speed" }
],
"templateid": "456434",
"discoveries": []
}
]
}
My code:
output = requests.post(url, data=apache_data, headers=headers)
content_out = json.loads(output.content)
content_out = json.dumps(content_out)
for key in content_out .items():
print(key)
Error: AttributeError: 'str' object has no attribute 'items'
i want the "name":"windows" ............ until "discoveries": []} as one dictionary . Like that "name": "linux_server"........ until "discoveries": []} as a another dictionary.. Any help would be appreciated.
Are you using requests? You can get access to the JSON as a dictionary by calling output.json()
So I think with your code, it would look something like this:
output = requests.post(url, data=apache_data, headers=headers)
for result in output.json()['result']:
print(result['name'], result['status'], result['templateid'])
And here's the output:
Windows 3 456434
linux_server 3 456434
In your example, Windows and linux_server are two dictionaries in the result list.
You've left in a line of debug / experiment code that converts the parsed JSON that you wanted back into a string. Try removing that:
output = requests.post(url, data=apache_data, headers=headers )
# This parses your JSON correctly.
content_out = json.loads(ouput.content)
# This line turns your parsed JSON back into a string. You don't need this.
# content_out = json.dumps(content_out)
for key in content_out.items():
print (key)

Connecting many json files to one

i get many json strings from a mysql DB an should combine them.
For example:
{
"type": "device",
"name": "Lampe",
"controls": [
{
"type": "switch",
"name": "Betrieb",
"topic": "/lampe/schalter"
}
]
}
in combination this devices should get into a array of a json file
{
"name": "Test-System",
"devices": [
{
"type": "device",
"name": "Lampe",
"controls": [
{
"type": "switch",
"name": "Betrieb",
"topic": "/lampe/schalter"
}
]
},
{
other Device
}
]
}
i do not understand how to do this in python
does someone have a idea how to do it ?
The json module can be used.
#!/usr/bin/env python3.5
import json
# Parse each device JSON file.
device1 = json.load(open("device-switch-Lampe.json"))
device2 = json.load(open("device-sensor-Wert.json"))
# more devices ...
obj = {"name": "Test-System", "devices": [device1, device2]}
print(json.dumps(obj))
Output (prettified):
{
"devices": [{
"type": "device",
"controls": [{
"type": "switch",
"topic": "/lampe/schalter",
"name": "Betrieb"
}],
"name": "Lampe"
}, {
"type": "device",
"controls": [{
"type": "sensor",
"topic": "/sensor/wert",
"name": "Wert"
}],
"name": "Sensor"
}],
"name": "Test-System"
}
There are two ways you could do this - by working on strings, or by working with Python-JSON data structures. The former would be something like
# untested code
s = '''{
"name": "Test-System",
"devices": [ '''
while True:
j = get_json_from_DB()
if not j: break # null string or None
s = s + j + ',\n'
s = s[:-2] + ']\n}\n' #[:-2 loses the last ',\n' from the loop
Or if you want to work with Python loaded-JSON then
import json
# untested code
s = {
"name": "Test-System",
"devices": []
}
while True:
j = get_json_from_DB()
if not j: break # null string or None
s['devices'].append( json.loads(j) )
# str = json.dumps(s) # ought to be valid
This latter will validate all your incoming json-strings (json.loads() will throw an exception for any bad JSON) and will be more efficient for large numbers of devices. It's therefore to be preferred unless you are working in a RAM-constrained embedded system with small numbers of devices, where the greater memory footprint of the latter is a problem.

Categories

Resources