IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Shell et commandes GNU Discussion :

Parser JSON avec JQ


Sujet :

Shell et commandes GNU

  1. #1
    Membre éclairé Avatar de Lekno
    Femme Profil pro
    Étudiant
    Inscrit en
    Septembre 2010
    Messages
    883
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 883
    Points : 761
    Points
    761
    Par défaut Parser JSON avec JQ
    Bonjour à tous, je souhaiterai parser un JSON en bash,

    L'objectif étant de trouver l'élément ayant la valeur la plus élevé dans le "profitability24".
    Je parviens à récupérer l'élément le plus élevé, ainsi que son identifiant dans la liste ci-dessous, mais je ne parviens pas à cibler l'élement via cet ID

    Si quelqu'un sait m'indiquer comment je peux récupérer toute les informtions sur un élement ayant la valeure la plus élevé sur : profitability24


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
     
     
    #Je recupere tout les estimated reward sur 24h et jextrais le plus élevé
    myarray=$(jq '.coins[] | .profitability24' ./coins.json | tr -d '"')
    ARRAY=($myarray)
    currel_max=0
    selected=0
    k=0
    for i in ${ARRAY[*]}
    do
    	if test $currel_max \< "$i";then
    		currel_max=$i
    		selected=$k
    	fi
    	k=$(($k + 1))
    done
    #currel_max contient la valeure la plus élevé de mon tableaux
    echo "$currel_max en position $selected du array (n+1 pour la liste)"
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
     
     
    {  
       "coins":{  
          "Metaverse":{  
             "id":209,
             "tag":"ETP",
             "algorithm":"Ethash",
             "block_time":"32.0",
             "block_reward":2.85,
             "block_reward24":2.84999999999999,
             "last_block":735942,
             "difficulty":12863182405700.0,
             "difficulty24":12985936350813.3,
             "nethash":401974450178,
             "exchange_rate":0.000478,
             "exchange_rate24":0.00044641652631579,
             "exchange_rate_vol":166.104947826329,
             "exchange_rate_curr":"BTC",
             "market_cap":"$141,062,778.41",
             "estimated_rewards":"2.23908",
             "estimated_rewards24":"2.21792",
             "btc_revenue":"0.00107028",
             "btc_revenue24":"0.00106017",
             "profitability":113,
             "profitability24":109,
             "lagging":false,
             "timestamp":1511434085
          },
          "Ethereum":{  
             "id":151,
             "tag":"ETH",
             "algorithm":"Ethash",
             "block_time":"13.7765",
             "block_reward":2.91,
             "block_reward24":2.91000000000001,
             "last_block":4606506,
             "difficulty":1.53698537894249e+15,
             "difficulty24":1.49834402729339e+15,
             "nethash":111565737229520,
             "exchange_rate":0.04941501,
             "exchange_rate24":0.0456098971428571,
             "exchange_rate_vol":7898.71459906,
             "exchange_rate_curr":"BTC",
             "market_cap":"$38,373,610,360.68",
             "estimated_rewards":"0.01914",
             "estimated_rewards24":"0.01963",
             "btc_revenue":"0.00094576",
             "btc_revenue24":"0.00097015",
             "profitability":100,
             "profitability24":100,
             "lagging":false,
             "timestamp":1511434100
          },
          "EthereumClassic":{  
             "id":162,
             "tag":"ETC",
             "algorithm":"Ethash",
             "block_time":"14.2584",
             "block_reward":4.85,
             "block_reward24":4.84999999999996,
             "last_block":4888990,
             "difficulty":118661392446502.0,
             "difficulty24":120339872092159.0,
             "nethash":8322209535887,
             "exchange_rate":0.00222754,
             "exchange_rate24":0.00217113590336135,
             "exchange_rate_vol":523.38750217,
             "exchange_rate_curr":"BTC",
             "market_cap":"$1,762,498,421.09",
             "estimated_rewards":"0.41317",
             "estimated_rewards24":"0.4074",
             "btc_revenue":"0.00092035",
             "btc_revenue24":"0.00090751",
             "profitability":97,
             "profitability24":94,
             "lagging":false,
             "timestamp":1511434100
          },
          "Nicehash-Ethash":{  
             "id":15,
             "tag":"NICEHASH",
             "algorithm":"Ethash",
             "block_time":1,
             "block_reward":1,
             "block_reward24":1,
             "last_block":0,
             "difficulty":1,
             "difficulty24":1,
             "nethash":2970180494550,
             "exchange_rate":0.0073,
             "exchange_rate24":0.0073,
             "exchange_rate_vol":23.272148727065,
             "exchange_rate_curr":"BTC",
             "market_cap":"$0.00",
             "estimated_rewards":"0.00085",
             "estimated_rewards24":"0.00085",
             "btc_revenue":"0.0008541",
             "btc_revenue24":"0.0008541",
             "profitability":90,
             "profitability24":88,
             "lagging":false,
             "timestamp":1511434152
          },
          "Musicoin":{  
             "id":178,
             "tag":"MUSIC",
             "algorithm":"Ethash",
             "block_time":"14.0",
             "block_reward":246.2,
             "block_reward24":246.199999999998,
             "last_block":1465046,
             "difficulty":4761757017160.0,
             "difficulty24":4665143102305.39,
             "nethash":340125501225,
             "exchange_rate":1.6e-06,
             "exchange_rate24":1.59831932773109e-06,
             "exchange_rate_vol":16.66860257,
             "exchange_rate_curr":"BTC",
             "market_cap":"$5,959,771.21",
             "estimated_rewards":"522.48167",
             "estimated_rewards24":"533.29833",
             "btc_revenue":"0.00083597",
             "btc_revenue24":"0.00085328",
             "profitability":88,
             "profitability24":88,
             "lagging":false,
             "timestamp":1511434082
          },
          "Ubiq":{  
             "id":173,
             "tag":"UBQ",
             "algorithm":"Ethash",
             "block_time":"87.0",
             "block_reward":8,
             "block_reward24":8.0,
             "last_block":297277,
             "difficulty":15822108736183.0,
             "difficulty24":16242338832427.6,
             "nethash":181863318806,
             "exchange_rate":0.000162,
             "exchange_rate24":0.000162110147058823,
             "exchange_rate_vol":35.0207546,
             "exchange_rate_curr":"BTC",
             "market_cap":"$51,111,773.99",
             "estimated_rewards":"5.10794",
             "estimated_rewards24":"4.97587",
             "btc_revenue":"0.00082749",
             "btc_revenue24":"0.00080609",
             "profitability":87,
             "profitability24":83,
             "lagging":false,
             "timestamp":1511434100
          },
          "Pirl":{  
             "id":211,
             "tag":"PIRL",
             "algorithm":"Ethash",
             "block_time":"14.0",
             "block_reward":10.0,
             "block_reward24":10.0,
             "last_block":335461,
             "difficulty":5683601971311.0,
             "difficulty24":6135607428330.71,
             "nethash":405971569379,
             "exchange_rate":4.8e-05,
             "exchange_rate24":4.94541596638656e-05,
             "exchange_rate_vol":4.64497169638992,
             "exchange_rate_curr":"BTC",
             "market_cap":"$1,564,363.27",
             "estimated_rewards":"17.78078",
             "estimated_rewards24":"16.47123",
             "btc_revenue":"0.00085348",
             "btc_revenue24":"0.00079062",
             "profitability":90,
             "profitability24":81,
             "lagging":false,
             "timestamp":1511434095
          },
          "Expanse":{  
             "id":154,
             "tag":"EXP",
             "algorithm":"Ethash",
             "block_time":"42.0",
             "block_reward":4.0,
             "block_reward24":4.0,
             "last_block":816354,
             "difficulty":11585690580128.0,
             "difficulty24":11253225910975.2,
             "nethash":275849775717,
             "exchange_rate":0.0002185,
             "exchange_rate24":0.000219143697478992,
             "exchange_rate_vol":24.59769884,
             "exchange_rate_curr":"BTC",
             "market_cap":"$13,988,645.11",
             "estimated_rewards":"3.48862",
             "estimated_rewards24":"3.59164",
             "btc_revenue":"0.00076226",
             "btc_revenue24":"0.00078477",
             "profitability":81,
             "profitability24":81,
             "lagging":false,
             "timestamp":1511433220
          },
          "Soilcoin":{  
             "id":189,
             "tag":"SOIL",
             "algorithm":"Ethash",
             "block_time":"55.0",
             "block_reward":3.0,
             "block_reward24":3.0,
             "last_block":3006722,
             "difficulty":68012393787.0,
             "difficulty24":68066897439.8518,
             "nethash":1236588977,
             "exchange_rate":8.1e-07,
             "exchange_rate24":8.10819327731093e-07,
             "exchange_rate_vol":0.0330886903266396,
             "exchange_rate_curr":"BTC",
             "market_cap":"$37,399.13",
             "estimated_rewards":"407.35341",
             "estimated_rewards24":"407.0554",
             "btc_revenue":"0.00032996",
             "btc_revenue24":"0.00032971",
             "profitability":35,
             "profitability24":34,
             "lagging":false,
             "timestamp":1511434096
          }
       }
    }

  2. #2
    Modérateur
    Avatar de N_BaH
    Profil pro
    Inscrit en
    Février 2008
    Messages
    7 549
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 7 549
    Points : 19 376
    Points
    19 376
    Par défaut
    Bonjour,

    \< n'est pas un opérateur de comparaison arithmétique (cf. help test)

    récupérer toute les informtions sur un élement
    c'est-à-dire ? toutes les valeurs de id, tag...timestamp ?

    jq ne pourrait pas le faire, sans passer par bash ?
    .
    N'oubliez pas de consulter les cours shell, la FAQ, et les pages man.

  3. #3
    Expert éminent Avatar de BufferBob
    Profil pro
    responsable R&D vidage de truites
    Inscrit en
    Novembre 2010
    Messages
    3 035
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : responsable R&D vidage de truites

    Informations forums :
    Inscription : Novembre 2010
    Messages : 3 035
    Points : 8 400
    Points
    8 400
    Par défaut
    salut,

    Citation Envoyé par N_BaH Voir le message
    jq ne pourrait pas le faire, sans passer par bash ?
    si carrément, c'est même à ça que ça sert, mais j'avoue que la syntaxe est un poil rebutante

    sur la base du fichier que tu donnes en exemple, un truc dans ce genre là :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $ jq '[.coins | to_entries[]] | max_by(.value.profitability24) | {(.key): .value}' fichier.json
    et le résultat :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    {
      "Metaverse": {
        "id": 209,
        "tag": "ETP",
        "algorithm": "Ethash",
        "block_time": "32.0",
        "block_reward": 2.85,
        "block_reward24": 2.84999999999999,
        "last_block": 735942,
        "difficulty": 12863182405700,
        "difficulty24": 12985936350813.3,
        "nethash": 401974450178,
        "exchange_rate": 0.000478,
        "exchange_rate24": 0.00044641652631579,
        "exchange_rate_vol": 166.104947826329,
        "exchange_rate_curr": "BTC",
        "market_cap": "$141,062,778.41",
        "estimated_rewards": "2.23908",
        "estimated_rewards24": "2.21792",
        "btc_revenue": "0.00107028",
        "btc_revenue24": "0.00106017",
        "profitability": 113,
        "profitability24": 109,
        "lagging": false,
        "timestamp": 1511434085
      }
    }
    ps: du coup va falloir qu'on partage les bénefs

  4. #4
    Membre éclairé Avatar de Lekno
    Femme Profil pro
    Étudiant
    Inscrit en
    Septembre 2010
    Messages
    883
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 34
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 883
    Points : 761
    Points
    761
    Par défaut
    super merci beaucoup pour les exemples

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Parser du JSON avec QJsonObject
    Par Socomfr dans le forum Qt
    Réponses: 2
    Dernier message: 15/05/2013, 11h13
  2. Parser JSON -> Java avec Jackson
    Par ProAccount dans le forum Format d'échange (XML, JSON...)
    Réponses: 0
    Dernier message: 28/11/2012, 18h04
  3. Parser le json avec la resultat de ma recherche
    Par moezBH dans le forum Services Web
    Réponses: 2
    Dernier message: 26/09/2011, 16h16
  4. Parser un JSON avec des variables avec des "-" et des ":"
    Par Quentin33 dans le forum Windows Phone
    Réponses: 2
    Dernier message: 22/05/2011, 01h04
  5. Parser JSON avec $.getJSON jquery adaptation code
    Par MatthieuFourcade dans le forum jQuery
    Réponses: 2
    Dernier message: 22/06/2009, 10h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo