I have a json file which is split into 3 sections..
{
"columns": {
"0": "Account Number",
"1": "Airport",
"2": "Terminal",
},
"rows": [
[
[
{
"v": "1234 "
},
{
"v": "LHR - London Heathrow"
},
{
"v": "T3"
}
]
]
]
,"types": [
{
"0": "TEXT",
"1": "TEXT",
"2": "TEXT"
}
]
}
what i want it to be like is this :
{
"Account Number" : "1234",
"Airport" : "LHR - London Heathrow",
"Terminal" : "T3"
}
How can acheive this please?
Dependencies
import json
import ast
Reading Json as file
with open("file.json") as f:
data = ast.literal_eval(f.read())
Reading Json as String
askersString = """{
"columns": {
"0": "Account Number",
"1": "Airport",
"2": "Terminal",
},
"rows": [
[
[
{
"v": "1234 "
},
{
"v": "LHR - London Heathrow"
},
{
"v": "T3"
}
]
]
]
,"types": [
{
"0": "TEXT",
"1": "TEXT",
"2": "TEXT"
}
]
}"""
data = ast.literal_eval(askersString)
Creating new json
columns = data["columns"]
a = data["rows"][0][0]
newJson = {}
for k, v in columns.items():
newJson[v] = a[int(k)]['v']
updatedJson = json.dumps(newJson, indent=4)
print(updatedJson)
Output
{
"Account Number": "1234 ",
"Airport": "LHR - London Heathrow",
"Terminal": "T3"
}
Related
I have some JSON data like:
[
{
"adset_id": "23851149362570451",
"reach": "862",
"clicks": "1",
"actions": [
{
"action_type": "post_reaction",
"value": "1"
},
{
"action_type": "post_engagement",
"value": "1"
},
{
"action_type": "page_engagement",
"value": "1"
}
],
"date_start": "2022-10-06",
},
]
In the actual data, this array would contain around 30 or 40 objects; I have shown just one for reference.
How can I flatten everything inside the "actions" array, so that it looks like this instead?
[
{
"adset_id": "23851149362570451",
"reach": "862",
"clicks": "1",
"post_reaction" : "1",
"post_engagement" : "1",
"page_engagement" : "1",
"date_start": "2022-10-06",
},
]
I quickly wrote this. Modify it as per how you ingest the data:
import json
final_result = []
with open('data.json') as f:
api_data_dict = json.load(f)
for api_data_obj in api_data_dict:
actions = []
values = []
updated_actions = {}
#assuming there are objects with action_type and value present inside this dict
for action in api_data_obj['actions']:
actions.append(action['action_type'])
values.append(action['value'])
for action, value in zip(actions, values):
updated_actions[action] = value
api_data_obj.pop('actions', None)
final_result.append({**api_data_obj, **updated_actions})
print(final_result)
data.json contents:
[{
"adset_id": "23851149362570451",
"reach": "862",
"clicks": "1",
"actions": [
{
"action_type": "post_reaction",
"value": "1"
},
{
"action_type": "post_engagement",
"value": "1"
},
{
"action_type": "page_engagement",
"value": "1"
}
],
"date_start": "2022-10-06"
},
{
"adset_id": "234543535643543",
"reach": "862",
"clicks": "1",
"actions": [
{
"action_type": "post_reaction",
"value": "2"
},
{
"action_type": "post_engagement",
"value": "2"
},
{
"action_type": "page_engagement",
"value": "2"
}
],
"date_start": "2022-10-06"
}]
Result:
[
{
"adset_id":"23851149362570451",
"reach":"862",
"clicks":"1",
"date_start":"2022-10-06",
"post_reaction":"1",
"post_engagement":"1",
"page_engagement":"1"
},
{
"adset_id":"234543535643543",
"reach":"862",
"clicks":"1",
"date_start":"2022-10-06",
"post_reaction":"2",
"post_engagement":"2",
"page_engagement":"2"
}
]
I have a Following Json File format in which the data will be having Dynamic update.
I have to parse the Json file using python with the following scenario
If the status: "PASS", then the results of value should be maths,q1 ,q2
Kindly me with this scenario using python
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket"
],
"answer": "Huston Rocket"
},
"status": "BLOCK"
},
"maths": {
"q1": {
"question": "5 + 7 = ?",
"options": [
"10",
"11",
"12",
"13"
],
"answer": "12"
},
"q2": {
"question": "12 - 8 = ?",
"options": [
"1",
"2",
"3",
"4"
],
"answer": "4"
},
"status": "PASS"
}
}
}
Hope below code gives you the expected output
import json
file = open('data.json')
data = json.load(file)
keys = []
for i in data['quiz']:
if data['quiz'][i]['status'] != 'PASS':
keys.append(i)
for key in keys:
del data['quiz'][key]
print(data)
for key in data['quiz'].keys():
print(key)
for key1 in data['quiz'][key].keys():
if key1 != 'status':
print(key1)
How can I read this JSON file into python pandas as a Data Frame. The main focus is on "Names" variable. I have created this sample JSON. I am trying to parse this JSON. All the keys need to be converted as a column name and values as data. I am struggling in doing this as the feature "Names" has multiple nested JSON objects. Thanks in Advance.
{
"entityType": "Entity",
"isreal": true,
"ispresent": false,
"isExtract": false,
"executionId": "3d0hy143-f58e-4fe7-8d83-3e1a011d448c",
"controlNumber": "on1072226092",
"field979": [
"$xNEW",
"$520200877062234"
],
"TagNumbers": [
"9781355445025",
"1375323029"
],
"flrs": [],
"atsNumbers960": [
"moo3452842",
"moo3452842",
"moo3452842",
"moo3452842",
"moo3452842"
],
"Names": [
{
"tag": "King",
"data": "03190cam a2200589Ii 4500"
},
{
"type": "controller",
"tag": "007",
"data": "on1071233092",
"ind1": " ",
"ind2": " "
},
{
"type": "controlfield",
"tag": "777",
"data": "181126s2019 nyuab 6 000 1 eng d",
"ind1": " ",
"ind2": " "
},
{
"tag": "000",
"ind1": " ",
"ind2": " ",
"subfields": [
{
"code": "a",
"data": "NEW"
},
{
"code": "b",
"data": "spa"
},
{
"code": "c",
"data": "new"
},
{
"code": "d",
"data": "VP#"
},
{
"code": "d",
"data": "OCLCF"
}
]
},
{
"tag": "020",
"ind1": " ",
"ind2": " ",
"subNames": [
{
"code": "a",
"data": "9733302915025"
}
]
},
{
"tag": "020",
"ind1": " ",
"ind2": " ",
"subfields": [
{
"code": "a",
"data": "1302915029"
}
]
}
}
I'm trying to transform the JSON from this:
{
"meta": {
"ver": "3.0"
},
"cols": [
{
"name": "val"
}
],
"rows": [
"cols": [
{
"name": "ts"
},
{
"name": "v0"
},
{
"name": "v1"
},
{
"name": "v2"
},
{
"name": "v3"
},
{
"ts": {
"_kind": "dateTime",
"val": "2021-07-07T00:10:00-07:00",
"tz": "Los_Angeles"
},
"v3": {
"_kind": "number",
"val": 6167699.5,
"unit": "kWh"
}
},
{
"ts": {
"_kind": "dateTime",
"val": "2021-07-07T00:15:00-07:00",
"tz": "Los_Angeles"
},
"v0": {
"_kind": "number",
"val": 808926.0625,
"unit": "m\\u00b3"
},
"v1": {
"_kind": "number",
"val": 112999.3046875,
"unit": "m\\u00b3"
},
"v2": {
"_kind": "number",
"val": 8823498,
"unit": "kWh"
}
}
]
}
to a more simplified form using the pyjq module:
{
"data": {
"v0": [
[
"first timestamp",
val
],
[
"second timestamp",
val
]
],
"v1": [
[
"first timestamp",
val
],
[
"second timestamp",
val
]
]
}
}
I got started with the pyjq module, however I'm unsure about how to proceed with place two values (one str, one float) within the [] separated by a comma. Here's my code (returns error as expected).
import json
import pyjq
with open('file.json') as f:
data = json.load(f)
transformed = pyjq.all('{data: { meter_id_1: [[[.rows[].val.rows[].ts.val + "," + .rows[].val.rows[].v0.val]]}}', data)
Thanks in advance.
I am trying to parse the following student grade report JSON using Python
{
"report":[
{
"enrollment": "rit2011001",
"name": "Julia",
"subject":[
{
"code": "DSA",
"grade": "A"
}
]
},
{
"enrollment": "rit2011020",
"name": "Samantha",
"subject":[
{
"code": "COM",
"grade": "B"
},
{
"code": "DSA",
"grade": "A"
}
]
}
]
}
Such that the report should be ordered ascending first by code, then by grade and then by enrollment. Here is how the output should be
COM B rit2011020 Samantha
DSA A rit2011001 Julia
DSA A rit2011020 Samantha
Here is my incomplete piece of code that I need help with:
import json
data='''{
"report":[
{
"enrollment": "rit2011001",
"name": "Julia",
"subject":[
{
"code": "DSA",
"grade": "A"
}
]
},
{
"enrollment": "rit2011020",
"name": "Samantha",
"subject":[
{
"code": "COM",
"grade": "B"
},
{
"code": "DSA",
"grade": "A"
}
]
}
]
}'''
print data #for debug
parsed_json = json.loads(data)
print parsed_json #for debug
for key,value in sorted(parsed_json.items()):
print key,value
I don't know how to apply successive filtering to achieve the result.
Try using a nested loop, with a print:
import json
data='''{
"report":[
{
"enrollment": "rit2011001",
"name": "Julia",
"subject":[
{
"code": "DSA",
"grade": "A"
}
]
},
{
"enrollment": "rit2011020",
"name": "Samantha",
"subject":[
{
"code": "COM",
"grade": "B"
},
{
"code": "DSA",
"grade": "A"
}
]
}
]
}'''
print data #for debug
parsed_json = json.loads(data)
print parsed_json #for debug
for i in parsed_json['report']:
for x in i['subject']:
print x['code'],x['grade'],i['enrollment'],i['name']
Output:
DSA A rit2011001 Julia
COM B rit2011020 Samantha
DSA A rit2011020 Samantha
If care about the order of the frame:
import json
data='''{
"report":[
{
"enrollment": "rit2011001",
"name": "Julia",
"subject":[
{
"code": "DSA",
"grade": "A"
}
]
},
{
"enrollment": "rit2011020",
"name": "Samantha",
"subject":[
{
"code": "COM",
"grade": "B"
},
{
"code": "DSA",
"grade": "A"
}
]
}
]
}'''
print data #for debug
parsed_json = json.loads(data)
print parsed_json #for debug
l=[]
for i in parsed_json['report']:
for x in i['subject']:
l.append(' '.join([x['code'],x['grade'],i['enrollment'],i['name']]))
print('\n'.join(sorted(l)))
If you are willing to use a very popular external library for data analysis then you can use pandas with json_normalize(), e.g.:
In []:
from pandas.io.json import json_normalize
df = json_normalize(parsed_json['report'], 'subject', ['enrollment', 'name'])
df.sort_values(['code', 'grade', 'enrollment']).reset_index(drop=True)
Out[]:
code grade enrollment name
0 COM B rit2011020 Samantha
1 DSA A rit2011001 Julia
2 DSA A rit2011020 Samantha