How to iterate over JSON array? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have this JSON array with multiple roots:
[
{
"issuer_ca_id": 16418,
"issuer_name": "C=US, O=Let's Encrypt, CN=Let's Encrypt Authority X3",
"name_value": "sub.test.com",
"min_cert_id": 325717795,
"min_entry_timestamp": "2018-02-08T16:47:39.089",
"not_before": "2018-02-08T15:47:39"
},
{
"issuer_ca_id":9324,
"issuer_name":"C=US, O=Amazon, OU=Server CA 1B, CN=Amazon",
"name_value":"marketplace.test.com",
"min_cert_id":921763659,
"min_entry_timestamp":"2018-11-05T19:36:18.593",
"not_before":"2018-10-31T00:00:00",
"not_after":"2019-11-30T12:00:00"
}
]
I want to iterate over it and print issuer_name values in Python. Any solution, please?

Use the json package and load the json. Assuming it is a string in memory (as opposed to a .json file):
jsonstring = """
[
{
"issuer_ca_id": 16418,
"issuer_name": "C=US, O=Let's Encrypt, CN=Let's Encrypt Authority X3",
"name_value": "sub.test.com",
"min_cert_id": 325717795,
"min_entry_timestamp": "2018-02-08T16:47:39.089",
"not_before": "2018-02-08T15:47:39"
},
{
"issuer_ca_id":9324,
"issuer_name":"C=US, O=Amazon, OU=Server CA 1B, CN=Amazon",
"name_value":"marketplace.test.com",
"min_cert_id":921763659,
"min_entry_timestamp":"2018-11-05T19:36:18.593",
"not_before":"2018-10-31T00:00:00",
"not_after":"2019-11-30T12:00:00"
}
]"""
import json
j = json.loads(jsonstring)
[item["issuer_name"] for item in j]
Gives:
["C=US, O=Let's Encrypt, CN=Let's Encrypt Authority X3",
'C=US, O=Amazon, OU=Server CA 1B, CN=Amazon']
Now, these don't look like names to me, but that's what is assigned to the issuer_name field, so I think that's something you have to take up with the owner of the data.
If it's a file, you do the loading in this basic pattern:
# something like this
with open("jsonfile.json", "rb") as fp:
j = json.load(fp)
See the docs here: https://docs.python.org/3.7/library/json.html

Related

Is there way to parse json data in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
import json
f = open('C:\Users\Hello\Desktop\usecase2.json',encoding = 'utf-8')
data = json.load(f)
print(data)
-Raw Data sheet contains the raw data to be parsed and arrange as per "Desired Output" sheet
-The number of iteration data for each Pur Lot is not same, however it is required to capture the entire raw da -you have to create a JSON file to capture entire data of Raw Data with the key as Pur Lot.
data
desired output
If you're looking to convert JSON (essentially text) to a python dict , then I would suggest you use this:
**
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
**
then you can call y['keys'] = values to use data
here , y['name'] = "John"

How to read JSON files and store values into a variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am working on a project which outputs a JSON file with certain characteristics, example below. How can I extract the values that are assigned to the "top", "left", "width" and "height" and assign these values to separate variables within Python3.7.
Detection result:
detection_02
JSON:
[
{
"faceId": "4c1cb007-ed71-42d4-b2af-7abc3e82210e",
"faceRectangle": {
"top": 76,
"left": 446,
"width": 226,
"height": 284
},
"faceAttributes": null,
"faceLandmarks": null
}
]
import json
json_data = json.loads(data_to_load)
top = json_data[0]['faceRectangle']['top']
left = json_data[0]['faceRectangle']['left']
width = json_data[0]['faceRectangle']['width']
height = json_data[0]['faceRectangle']['height']

JSON array convert to Python array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
[{
data: "31/05/2010 19:39:00",
empresa: {
codigo: 1
},
descricao: "teste",
tipoEvento: "RHYTHM VARIATION"
},
{
data: "31/05/2010 18:15:00",
empresa: {
codigo: 1
},
descricao: "teste ",
tipoEvento: "RHYTHM VARIATION"
}]
This is an overly simple question, and I would encourage you to read the documentation in the future, but nonetheless:
import json
your_str_data = r'''
[{"data":"31/05/2010 19:39:00","empresa":{"codigo":1},"descricao":"teste","tipoEvento":"RHYTHM VARIATION"},{"data":"31/05/2010 18:15:00","empresa":{"codigo":1},"descricao":"teste ","tipoEvento":"RHYTHM VARIATION"}]
'''
jdata = json.loads(your_str_data)
import pprint
pprint.pprint(jdata)

Convert string structure in another [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a file with this pattern :
[account.invoice.set_num]
job_size = 0
trans_size = 100
[commission.invoice_second.create_full]
j_size = 0
[commission.invoice_principal.finalize]
j_size = 12
in_directory = /to/the/directory
I want to transform this pattern to a text like :
ACCOUNT_INVOICE_SET_NUM_JOB_SIZE = 0
ACCOUNT_INVOICE_SET_NUM_TRANS_SIZE = 100
COMMISSION_INVOICE_SECOND_CREATE_FULL_J_SIZE=0
COMMISSION_INVOICE_PRINCIPALE_FINALIZE_J_SIZE=12
COMMISSION_INVOICE_PRINCIPALE_FINALIZE_IN_DIRECTORY=/to/the/directory
I try to do that in Bash unix or in Python.
I don't konw what is the best/easiest way to do that.
It's quite feasible with config.ConfigParser features:
from configparser import ConfigParser
config = ConfigParser()
config.read('yourfile')
config_lines = ''
for section in config.sections():
s_key = section.replace('.', '_') # transformed section key
for k, v in config.items(section):
config_lines += f'{s_key}_{k}'.upper() + f'={v}\n'
print(config_lines)
The output:
ACCOUNT_INVOICE_SET_NUM_JOB_SIZE=0
ACCOUNT_INVOICE_SET_NUM_TRANS_SIZE=100
COMMISSION_INVOICE_SECOND_CREATE_FULL_J_SIZE=0
COMMISSION_INVOICE_PRINCIPAL_FINALIZE_J_SIZE=12
COMMISSION_INVOICE_PRINCIPAL_FINALIZE_IN_DIRECTORY=/to/the/directory

MongoDB python how parse multi-level json [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
how parse multi-level json data, in Mongodb with Python 3.5?
PLAYER_DEFAULT = {
"_id": Inc(PlayersDB),
"mail": "test#gmail.com",
"password": "123456",
"token": uuid4().hex,
"username": "applet",
"user_info":
{
"show_name" : "Hero",
"rate_us": 20000,
"rating": 300,
"gcg": "",
"ration":0,
"items":
[
{"id":1, "item_type":"socket", "name":"aEveA", "data":{"level":1, "stat":"AVA"}},
{"id":2, "item_type":"socket", "name":"aEveA", "data":{"level":4, "stat":"AVA"}},
{"id":3, "item_type":"socket", "name":"Hestos", "data":{"level":9, "stat":"Hest"}},
{"id":4, "item_type":"user", "name":"AAACZX", "data":{"expr":1000}},
{"id":5, "item_type":"user", "name":"AAAAZZZCX", "data": {"expr":1000}}
]
}
}
how get data level and stat in items?
["_id"]->["show_name"]
["_id"]->["user_info"]->["items"]
["_id"]->["user_info"]->["items"] -> get value by "stat" in items[0]
me need get items id, by items "stat" value
i use loop
how get x count?
for x in PlayersDB.find({"_id":1}):
print(x["user_info"]["items"][x.count()])
And... how to update "item_type", by "id", in "items"?
And how to delete 1 docuement by "id":1 in "items"?
For that document, try:
doc['user_info']['items'][0]['data']['stat']
If you have a doc for a player, and want to count how many items that player has, you will use some built-in Python function, which I will call XXX, and which you should research to see what that function is, and use it on the items attribute of the doc:
number_of_items = XXX(doc['user_info']['items'])
And in future, please use the same names and structure in your question as in your posted examples.

Categories