Python application for flattening and mapping nested JSON keys

I am completely new to JSON and struggling with understanding the structure of a JSON file.

Here is an example of a JSON file I have:

{"employeeId":{"0":02100, "1":02101, "2":02102,... "1000000":021000000},
 "employeeName":{"0":"Smith", "1":"John", "2":"Mark",... "1000000":"Dave"},
 "employeeDept":{"0":"Work", "1":"Art", "2":"Mop",... "1000000":"Clean"},
 "employeeAddress":"0":"CA", "1":"TX", "2":"UT",... "1000000":"DC"}

I want to convert this JSON into a flattened format using Python. Each set of data should be mapped as follows:

{"employeeId": 02100,
 "employeeName":"Smith",
 "employeeDept":"Work",
 "employeeAddress":"CA"},

{"employeeId": 02101,
 "employeeName":"John",
 "employeeDept":"Art",
 "employeeAddress":"TX"},

{"employeeId": 02102,
 "employeeName":"Mark",
 "employeeDept":"Mop",
 "employeeAddress":"UT"},
.
.
.
{"employeeId": 021000000,
 "employeeName":"Dave",
 "employeeDept":"Clean",
 "employeeAddress":"DC"}

I attempted to achieve this using Python by first creating a sample object:

sample_object1 = {"employeeId":{"0":"02100", "1":"02101", "2":"02102", "1000000":"021000000"},
 "employeeName":{"0":"Smith", "1":"John", "2":"Mark", "1000000":"Dave"},
 "employeeDept":{"0":"Work", "1":"Art", "2":"Mop", "1000000":"Clean"},
 "employeeAddress":{"0":"CA", "1":"TX", "2":"UT", "1000000":"DC"}}

from pandas.io.json import json_normalize
json_normalize(sample_object1)

This resulted in the following output:


employeeAddress.0   employeeAddress.1   employeeAddress.1000000 employeeAddress.2   employeeDept.0  employeeDept.1  employeeDept.1000000    employeeDept.2  employeeId.0    employeeId.1    employeeId.1000000  employeeId.2    employeeName.0  employeeName.1  employeeName.1000000    employeeName.2
0   CA  TX  DC  UT  Work    Art Clean   Mop 02100   02101   021000000   02102   Smith   John    Dave    Mark

Answer №1

Below is the solution provided:

details = {"studentId":{"0":"1066", "1":"1058", "2":"1047", "1000000":"1099"},
 "studentName":{"0":"Alice", "1":"Bob", "2":"Charlie", "1000000":"Daisy"},
 "studentSubject":{"0":"Math", "1":"Science", "2":"History", "1000000":"English"},
 "studentGrade":{"0":"A", "1":"B", "2":"C", "1000000":"A"}}
import pandas as pd
df = pd.DataFrame(details)
df.to_json(orient = "records")

Here is the expected outcome

'[{"studentId":"1066","studentName":"Alice","studentSubject":"Math","studentGrade":"A"},{"studentId":"1058","studentName":"Bob","studentSubject":"Science","studentGrade":"B"},{"studentId":"1099","studentName":"Daisy","studentSubject":"English","studentGrade":"A"},{"studentId":"1047","studentName":"Charlie","studentSubject":"History","studentGrade":"C"}]'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Object-Oriented Programming in Python: Classes and Inheritance

... from PyQt4.QtGui import * from PyQt4.QtCore import * class UserInfoModalWindow(QDialog): def init(self): super(UserInfoModalWindow, self).init() self.dialog_window = QDialog(self) ... ...

Loop through a collection of objects stored in a dictionary

In my current project, I have a collection of objects stored in a dictionary that represent specific "Names/Ranges" within a spreadsheet. As I traverse through the spreadsheet data, I encounter the need to update the values associated with these ranges. T ...

I need help resolving this issue with OpenShift: ImportError - libmemcached.so.11 file cannot be located

I am using a python 2.7 cartridge with the Django framework and I want to integrate memcached into it. I have added the Memcached Cloud cartridge to my app and followed this guide to set up my project. In order to utilize the Django caching backend ' ...

Is there a way to efficiently resize a collection of 2D arrays (stored as a 3D array) using a 2D array in NumPy for vectorized operations?

I have a 3D array consisting of N x N covariance matrices for M channels [M x N x N]. Additionally, I possess a 2D matrix containing scaling factors for each channel at various time points [M x T]. My goal is to generate a 4D array that includes a scaled v ...

Attempting to have my Python script execute a click on a button only if a specific condition is not met

Upon meeting the condition, a click is currently triggered. For example: The code scans for a specific word on my screen and compares it to predefined data set. Expected behavior: If the screen is empty, a click should happen. If the word "cookie" app ...

Challenges with arrays while using a for loop

I'm trying to transform an array A with 10 elements into an array B with 100 elements. Each element in B ranging from 0 to 9 should be the same as element 0 in A Each element in B ranging from 10 to 19 should be the same as element 1 in A .... Eac ...

Retrieve the text from the input field following a page refresh

When attempting to retrieve the value generated after clicking the submit button from an input box, I am facing an issue where the page reloads and does not provide any output. This is on the website I have attempted to use the .text option with the xpath ...

Approaches for transforming data and displaying the output in HTML using Django

My data is structured as follows: ws=[{'a': datetime.date(2020, 8, 31), 'b': <BB: 01>, 'c': <CC: 4>}, {'a': datetime.date(2020, 8, 31), 'b': <BB: 01>, 'c': <CC: 5>}, ...

Automate Citrix web tasks using Selenium or similar tools for efficient and streamlined processes

Are there any methods for implementing selenium or a similar tool on Citrix web systems? I have been relying on pyautogui, but it seems to be quite ineffective by itself. Any suggestions on how to enhance pyautogui would be greatly appreciated. ...

Retrieve the precise response parameter using urllib in Python

Is there a way to process the token information obtained from a web request using urllib in Python? from urllib import request from urllib.parse import urlencode response = request.urlopen(req, data=login_data) content = response.read() The response typic ...

JavaScript JSON YouTube API viewCount function is not functioning correctly

I'm struggling to retrieve the views of a video using JavaScript (Chrome Extension). Here is the code I have so far: $(function() { $.getJSON('http://gdata.youtube.com/feeds/api/videos/H542nLTTbu0?alt=json',function(data) { ...

Converting a hash of hashmaps to JSON format in Java

Hey there, I've got a nested object filled with hashes and lists. I'm attempting to utilize gson but it's not cooperating when the values in hashmaps are also hashmaps instead of simple strings. Map questionDetails = new HashMap<>(); ...

Using timedelta to filter data when a field may be missing

I'm attempting to develop a function within the PlayerAt table to retrieve records from a PlayerOc table based on a filter that utilizes the timedelta feature. Below is an abbreviated version of the class: class PlayerAt(Base): id_ = sa.Column(sa ...

Retrieving data from intricate JSON structures

Currently, I am engaged in web scraping to extract the "id" of all locations from a complex json content. Click here for the JSON link I attempted using the dict.items method, but it only extracted 2 values at the start of the dictionary followed by a li ...

A guide on retrieving the Subtotal Price with Selenium and Python

Struggling with extracting the necessary information using Xpath. Specifically, it involves obtaining the Price - Subtotal from the HTML Image attached. The xpath code I have been using is provided below. What could be missing? Ultimately, my goal is to st ...

Creating a formatted string list using values from a pandas DataFrame: A step-by-step guide

Issue Looking for a way to generate a list of strings with placeholders (similar to "f-strings") by leveraging the values in a pandas DataFrame. Scenario Consider the following dataframe: import pandas as pd data = [ ['Alice', 13, &apos ...

Converting JSON to CSV Using Python

i am currently working with a JSON file structured like this: { "temperature": [ { "ts": 1672753924545, "value": "100" } ], "temperature c1": [ { "ts": 167275392 ...

Could the absence of a tree view in the div be due to an error in the positioning of the DOM

I'm currently working on displaying a tree structure with specific child elements inside a div using JavaScript and HTML. Despite creating all the necessary objects and ensuring that the data is correctly read from the JSON file (refer to the "data" i ...

Is there a way to parse simple data types from JSON using ASP.NET's System.Runtime.Serialization.Json?

I am facing a unique challenge. When using DataContractJsonSerializer with complex types (custom types), it works well. However, I need to deserialize TimeStamp or DateTime from a string. Since I cannot mark these types with DataContract, DataMember attrib ...

Updating a class function's value using an external function in Python

Greetings! Here is a snippet of code showcasing a function named propertyValue within an object class titled CashOnCashROI. Additionally, there is a function called calculator defined outside the class. An instance of the class has been instantiated as "re ...