How to convert jsonl to json in python - python

I am looking for a way to convert a JSONL file to JSON.
I have written a python program but at some point it's just getting to slow because the JSON file gets to big. And with every loop I checked if the keys are in thereā€¦
Maybe some of you has an idea on how to write a program that does the following:
The JSONL file is from a Firebase export, which looks like this:
{"__path__":"cars/89dHLbXAOooudSNHv6Jt","__exportPath__":"","__id__":"89dHLbXAOooudSNHv6Jt","brandId":"Fp5Kwr7NXNRPxY7Yx3QK","name":"Testuser", "email": "test.user#email.com", "settings":{"test1":true,"test2":true}}
The document can also have subcollections:
{"__path__":"cars/89dHLbXAOooudSNHv6Jt/subcollection/dwadwadawdwaddwa","__exportPath__":"","__id__":"dwadwadawdwaddwa","modelId":"d54321242","name":"testcar", "motor": "example f1"
this needs to be converted into this:
{
"cars": {
"89dHLbXAOooudSNHv6Jt": {
"brandId": "Fp5Kwr7NXNRPxY7Yx3QK",
"name": "Testcar",
"email": "test.user#email.com",
"settings": {
"test1": true,
"test2": true
},
"subcollection": {
"dwadwadawdwaddwa": {
"modelId": "d54321242",
"name": "testcar",
"motor": "example f1"
}
}
}
}
}

Related

Python: turn JSON object to JSON array

I have the following dictionary in python which I'm saving into a file:
d2 = {
"CHARACTER": {
"IDENTITY": {
"FORM": {
"id": "BK1",
"type": "MAGE",
"role": "DARK"
}
},
"USER": {
"owner": {
"id": "SABBATH13"
},
"level": "16"
}
}
}
jsonfile = open('d2.json', 'w')
jsonfile.write(simplejson.dumps(d2, indent=4))
jsonfile.close()
However, I'm told this is a JSON object, which I need to turn into a JSON array of the form:
[{
"CHARACTER": {
"IDENTITY": {
"FORM": {
"id": "BK1",
"type": "MAGE",
"role": "DARK"
}
},
"USER": {
"owner": {
"id": "SABBATH13"
},
"level": "16"
}
}
}]
Which is essentially adding square brackets at the beginning and end.
What is the proper way to do this? Should I convert to string and add brackets, then convert back? Sorry, total JSON newbie here.
You're thinking at the wrong level of abstraction. It's not about the brackets, it's about that you have a data structure which is an object, when what you apparently need is a list/array of objects (even if there's just one object in the list). So:
d2 = [d2]
Now dumps this and you get what you need.

How to add a text at specific location in json file using python or bat?

I have a json file like as follows:
{
"category": {
"gender": {
"male": "A",
"female": "B"
},
"age": {
"young": 25
},
"dob": {
"dob_list": [
"crap"
]
}
},
"sample": {
"game1": {
"title": "<arg>",
"player": "john",
},
"game2": {
"title": "<arg>",
"game_location": "C:/game/<arg>/crap.exe",
"game_root": "C:/games/"
}
}
}
So i want to have some runtime arguments against "arg" in above json file passed from some python script or bat file. So can anyone suggest me how can i acheive that and which option is better to pass values a python script or bat file?
you can use json.load() on the file and then manipulate the contents of the file using dict comprehension
with open("file.json",'r') as f:
mydict = json.load(f)
mydict['sample']['game1']['title'] = yourValue
with open('file.json','w') as f:
f.write(str(mydict))

how to read specific values using Json to dict?

"Instances": [{
"nlu_classification": {
"Domain": "UDE",
"Intention": "Unspecified"
},
"nlu_interpretation_index": 1,
"nlu_slot_details": {
"Name": {
"literal": "ConnectedDrive"
},
"Search-phrase": {
"literal": "connecteddrive"
}
},
"interpretation_confidence": 5484
}],
"type": "nlu_results",
"api_version": "1.0"
}],
"nlps_version": "nlps(z):6.1.100.12.2-B359;Version: nlps-base-Zeppelin-6.1.100-B124-GMT20151130193521;"
}
},
"final_response": 1,
"prompt": "",
"result_format": "appserver_post_results"
}
I am getting the above code as a reply from the server. I am storing those result in the variable NLU_RESULT. later I am using json_loads to convert that json_format into dict and to check for the specific value within it as below.
parsed_json = json.loads(NLU_RESULT)
print(parsed_json["Instances"]["nlu_classification"]["Domain"]).
when I use the above code. Its not printing the value of Domain. Can someone tell me what is the mistake here ?
UPDATE
it should be something like
parsed['appserver_results']['payload']['actions'][0]['Instances'][0]['nlu_classification']['Domain']
the json you posted has instances as an array
so it should be something like
print(parsed_json["Instances"][0]["nlu_classification"]["Domain"])
also the json is a bit broken and contains some array closing without the array

grab values in json

I have the following json in this format:
{
"HATg": {
"id": "208-2",
"code": "225a"
"state" : True
},
"PROPEMPTY": {
"id": "208-3",
"code": "225b"
"state" False
}
}
Was wondering how do I access/grab both the id and code as I iterate each items in the file in a pythonic way? Like for i in items...
By the way, the contents in the json file differ as it is manipulate by user adding in different contents. Apologize in advance if I am not using any terms as I am not sure what they are called
Assuming your "JSON" looks more like this:
{
"HATg": {
"id": "208-2",
"code": "225a",
"state": true
},
"PROPEMPTY": {
"id": "208-3",
"code": "225b",
"state": false
}
}
and that you have succesfully parsed it into a Python object (for example, by using j = json.load(jsonfile)), then it's trivial to iterate through it in Python (assuming Python 3):
>>> for key, value in j.items():
... print("{}: {}, {}".format(key, value['id'], value['code']))
...
PROPEMPTY: 208-3, 225b
HATg: 208-2, 225a
What you have here is a python dictionary and not a json. You can iterate them like this:
a = {
"HATg": {
"id": "208-2",
"code": "225a",
"state" : True
},
"PROPEMPTY": {
"id": "208-3",
"code": "225b",
"state" : False
}
}
for i in a:
print i
print a[i]['id'], a[i]['code']
This will give the output as
PROPEMPTY
208-3 225b
HATg
208-2 225a

decoding json string in python

I have the following JSON string (from wikipedia http://en.wikipedia.org/wiki/JSON)
{
"name":"Product",
"properties":
{
"id":
{
"type":"number",
"description":"Product identifier",
"required":true
},
"name":
{
"type":"string",
"description":"Name of the product",
"required":true
},
"price":
{
"type":"number",
"minimum":0,
"required":true
},
"tags":
{
"type":"array",
"items":
{
"type":"string"
}
},
"stock":
{
"type":"object",
"properties":
{
"warehouse":
{
"type":"number"
},
"retail":
{
"type":"number"
}
}
}
}
}
I am trying to decode this string using Python json library. I would like to access the node
properties - > stock - > properties - > warehouse.
I understand that json.loads() function stores the json string as a dictionary. But in this case properties is my key and everything under that are values. How do I access the above node.
import json
jsonText=""
file = open("c:/dir/jsondec.json")
for line in file.xreadlines():
jsonText+=line
data = json.loads(jsonText)
for k,v in data.items():
print k // shows name and properties
file.close();
Thanks
You can load json straight from the file like this:
f = open("c:/dir/jsondec.json")
data = json.load(f)
Based on your input string, data is now a dictionary that contains other dictionaries. You can just navigate up the dictionaries like so:
node = data['properties']['stock']['properties']['warehouse']
print str(node)

Categories

Resources