Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Python functions for flattening a JSON object to a single dictionary of pairs, and unflattening that dictionary back to a JSON object
Python functions for flattening a JSON object to a single dictionary of pairs, and unflattening that dictionary back to a JSON object.
This can be useful if you need to represent a JSON object using a regular HTML form or transmit it as a set of query string parameters.
For example:
>>> import json_flatten
>>> json_flatten.flatten({"foo": {"bar": [1, True, None]}})
{'foo.bar.[0]$int': '1', 'foo.bar.[1]$bool': 'True', 'foo.bar.[2]$none': 'None'}
>>> json_flatten.unflatten(_)
{'foo': {'bar': [1, True, None]}}
The top-level object passed to flatten()
must be a dictionary.
$type
suffixes.[index]
notation.For nested objects, keys are constructed by joining the nested keys with dots.
Example:
{
"user": {
"name": "John",
"age": 30
}
}
Flattened:
user.name=John
user.age$int=30
List items are represented using [index]
notation.
Example:
{
"fruits": [
"apple",
"banana",
"cherry"
]
}
Flattened:
fruits.[0]=apple
fruits.[1]=banana
fruits.[2]=cherry
For nested lists, the index notation is repeated.
Example:
{"matrix": [[1, 2], [3, 4]]}
Flattened:
matrix.[0].[0]$int=1
matrix.[0].[1]$int=2
matrix.[1].[0]$int=3
matrix.[1].[1]$int=4
Types are preserved using $type
suffixes:
Type | Suffix | Example |
---|---|---|
String | name=Cleo | |
Integer | $int | age$int=30 |
Float | $float | price$float=19.99 |
Boolean | $bool | active$bool=True |
Null | $none | data$none=None |
Empty object | $empty | obj$empty={} |
Empty list | $emptylist | list$emptylist=[] |
String values do not require a type suffix.
JSON:
{
"user": {
"name": "Alice",
"age": 28,
"hobbies": [
"reading",
"swimming"
],
"address": {
"street": "123 Main St",
"city": "Anytown"
},
"active": true,
"salary": 50000.5,
"spouse": null
}
}
Flattened:
user.name=Alice
user.age$int=28
user.hobbies.[0]=reading
user.hobbies.[1]=swimming
user.address.street=123 Main St
user.address.city=Anytown
user.active$bool=True
user.salary$float=50000.5
user.spouse$none=None
FAQs
Python functions for flattening a JSON object to a single dictionary of pairs, and unflattening that dictionary back to a JSON object
We found that json-flatten demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.