O JSONPARSE é uma biblioteca de análise JSON simples. Extraia o que é necessário da chave: pares de valor.
pip install jsonparseAqui está um exemplo rápido do que o JSONPARSE é capaz de fazer.
from jsonparse import find_key , find_keys , find_key_chain , find_key_value , find_value
data = [{
"key0" :
{
"key1" : "result" ,
"key2" :
{
"key1" : "result1" ,
"key3" : { "key1" : "result2" }
}
}
}]
find_key ( data , 'key1' )
[ 'result2' , 'result1' , 'result' ]
find_key_chain ( data , [ 'key0' , 'key2' , 'key3' , 'key1' ])
[ 'result2' ]➕ Veja documentação adicional na seção API abaixo.
pip install jsonparse Resumo dos comandos da CLI. Para informações completas, jp --help
Nota, jsonparse e jp são equivalentes.
jp key key1 --file text.json
jp keys key1 key2 key3 --file text.json
jp key-chain my '*' chain --file text.json
jp key-value key1 '"result"' --file text.json
echo '{"key1": {"key2": 5}}' | jp key key2
jp value null --file text.json
jp value 42 --file text.json
jp value '"strValue"' --file text.json
Os exemplos da API usando os seguintes dados de teste.
data = [
{ "key" : 1 },
{ "key" : 2 },
{ "my" :
{ "key" :
{
"chain" : "A" ,
"rope" : 5 ,
"string" : 1.2 ,
"cable" : False
}
}
},
{ "your" :
{ "key" :
{
"chain" : "B" ,
"rope" : 7 ,
"string" : 0.7 ,
"cable" : True
}
}
}
] from jsonparse import find_key , find_keys , find_key_chain , find_key_value , find_valueFind_Key ( Dados : Dict | List | OrdesedDict, Chave : STR ) -> Lista
Retornará todos os valores da chave correspondente.
find_key ( data , 'chain' )
[ 'A' , 'B' ]
find_key ( data , 'key' )
[ 1 , 2 , { 'chain' : 'A' , 'rope' : 5 , 'string' : 1.2 , 'cable' : False }, { 'chain' : 'B' , 'rope' : 7 , 'string' : 0.7 , 'cable' : True }]Find_keys ( Dados : Dict | List | OrderdDict, Keys : List, Grupo : Bool = True ) -> Lista
O valor de retorno padrão é uma lista bidimensional. [ [], [], ...] .
Para retornar todos os valores como uma lista unidimensional, set group=False .
A ordem das chaves não importa.
find_keys ( data , [ 'rope' , 'cable' ])
[[ 5 , False ], [ 7 , True ]]
find_keys ( data , [ 'rope' , 'cable' ], group = False )
[ 5 , False , 7 , True ]find_key_chain ( dados : dict | list | orderddict, chaves : list ) -> list
A cadeia -chave é uma lista ordenada de chaves. A cadeia precisa começar no nível raiz dos dados aninhados.
O curinga * pode ser usado como chave (s) para corresponder a qualquer.
find_key_chain ( data , [ 'my' , 'key' , 'chain' ])
[ 'A' ]
find_key_chain ( data , [ 'key' ])
[ 1 , 2 ]
find_key_chain ( data , [ '*' , 'key' , 'chain' ])
[ 'A' , 'B' ]
find_key_chain ( data , [ '*' , 'key' , '*' ])
[ 'A' , 5 , 1.2 , False , 'B' , 7 , 0.7 , True ]find_key_value ( dados : dict | list | orderddict, chave : str, valor : str | int | float | bool | nenhum) -> lista
A lista retornada contém os dicionários que contêm a chave especificada: par de valores.
find_key_value ( data , 'cable' , False )
[{ 'chain' : 'A' , 'rope' : 5 , 'string' : 1.2 , 'cable' : False }]
find_key_value ( data , 'chain' , 'B' )
[{ 'chain' : 'B' , 'rope' : 7 , 'string' : 0.7 , 'cable' : True }]find_value ( dados : dict | List | OrdesedDict, valor : str | int | float | bool | nenhum ) -> Lista
Retornará todas as chaves do valor correspondente.
find_value ( data , 'A' )
[ 'chain' ]
find_value ( data , False )
[ 'cable' ]Visite a documentação da API Swagger
Todos os pontos de extremidade são solicitações de postagem HTTP, onde você inclui os dados JSON pesquisáveis no órgão de solicitação.
POST /v1/key/{key}
POST /v1/keys ? key=1 & key=2 & key=3 & key=4...
POST /v1/keychain ? key=1 & key=2 & key=3 & key=4...
POST /v1/keyvalue ? key=a & value=1
POST /v1/value/{value}Vamos praticar o uso da API Pública, Free-to-Use-Não-Autenticação, Web hospedada no GCP Cloud Run.
Estamos postando os dados JSON com o CURL, solicitando procurar a chave, 'key1'. Os valores de chave encontrados são retornados como JSON.
curl -X POST " https://jsonparse.dev/v1/key/key1 "
-H ' Content-Type: application/json '
-d ' [{"key0":{"key1":"result","key2":{"key1":"result1","key3":{"key1":"result2"}}}}] '
[ " result2 " , " result1 " , " result " ]Ou (usando a biblioteca Python e solicitações)
import requests
data = [{
"key0" :
{
"key1" : "result" ,
"key2" :
{
"key1" : "result1" ,
"key3" : { "key1" : "result2" }
}
}
}]
requests . post ( 'https://jsonparse.dev/v1/key/key1' , json = data ). json ()
[ 'result2' , 'result1' , 'result' ]pip install " jsonparse[webapi] "
gunicorn -b 0.0.0.0:8000 jsonparse.webapi:appComo alternativa, execute o contêiner do Docker
docker run -d ctomkow/jsonparse