0

I have a json file and I have to extract only the value of the key "data" and decode the base64-encoded.

This is the json file

{
    "equno": "229151246954324320",
    "data": "CdwL703m6njjyJ7tdEXdRoYKKv49oatv9UWvdLONpWgd407fakXNfbt18j+qKb/toYgHZvj34ig7iu4XN7BfrqTf/wNsIdVNJ67cH9hCyIfULWLgyT01vQ7u5zS5vSB9hazXTQHRYGxpV+eCrVFMEdxHId54sJDn+KL8hea93WyQKlFwMDO0QQD5X2lK02Y88uI7MehBUFXzK5jNTgKmLtUE9KoM4xF6bEzm2oNMrxz0QwOB4b/tvRvhThb/r+Wgb32gV3UBZBZ/RP4ID+lc7JE7TLVRyeGCNA+uV11/no358XZdGo/E5Aq7KZ95W+rQ8TE3/PLbodAhWZ+1wAcXvfuxJxpIm0giOZv3Dys/pZesM5wbdwaNrFnD+ngHfXB67IxiBM3oRRxC7CBHoFvQFjC8g2E3dk1ELP6VPex24lPJY1JeBwuy8DQroN7rxa4bwLAE6Z3SyL16dpwYdMmAB2YN2h4nMGfl1TMXPGsJKxcSv4tBLj905WTGYgDKG3sQ0AR4YHoPKni7/rUZQb/hM25wXFKYNRzGU6EIleCTP4fl1vqASLFUHDS0GqjwcYkCOilvDbb3PqNqDzPEI84L7XDidiWQ8XKfzw7ryjuIaw1b1ODqN3+ctnny88WXTzANzwA5wjqfhDJDGpHr58fQgi1/j2QIsFBt+VoOslxvx1YQvbubDTwM7dTEwWDY0U5+l8KvSv9fYIMbNYxjwXU3tR2SFhClumNjisUE0lHgCBAfkbTA5Fw9eW3+QRZdB5rY/7DWgxlRnBORZ54c5xxTnsk2ntFm14zQA8HN7zv09FQW+sMk6B767cyzi5HoEkf+PjnNh78OrIPVOFtigMYUb5PdDWDzjDCu2+9dN4mm5aml+/SIOFDUHg6aX+GLj7c0tI0thMFAR6dKP6QtmVbUanF7gSt+L2c4qRq48s3QYMlrTr++PqeoCYNuhWIo2iXllvzERarLDU/pxZNfGB39bFmjmiAnLwmDqNZuVTi40/A38AI+r4f39Y/eywskz/rco1CZGUXxd0FJj0pwdO9H0eedwVgXAmi3KYy3j5MZBWeObqs/ufvRpHjDeh54Bq91DrxcKPya/b6FGDxH73jIgB9Y9x/mbZq2h20H9fbbV+hTk8XIA5ItY+2N9J7FHiJ+NyQbl4UNZT/GVF4HS+NXplgzEAEIlzgRwrNoY0GJzeocxZlAa5f5ANu7OHltqpSTAZ0PzVCopG1NgwaQEpS08mVAtgXo7jq34VejdNuHiTo+/ht3Dn+C+WzKXHZIABkhHjGg1Bv4hJHuLXIpQjIE0xwQo2UcTmcAYvrGO6FcHZz+eRUmJyrtsJczwZK7nimfgJ6T/iuggPVwyn9pifU9VA=="
}

I tried using jq

 jq -r '.[].data' < test.json | base64 --decode

But I got this error:

jq: error (at <stdin>:3): Cannot index string with string "data"

I have no idea how to resolve this error. I also tried using python but I couldn't decode it. Help me, please!

3
  • The [] is wrong, you don't have an array here.
    – tripleee
    Jul 16, 2020 at 6:09
  • Thanks! I edited it by 'jq -r '.data' < test.json | base64 --decode' and I got this. � �M��x�Ȟ�tE�F� *�=��o�E�t���h�N�jE�}�u�?�)���f���(;��7�_����l!�M'���Bȇ�-b��=5���4�� }���M�`liW炭QL�G! .... Maybe the data is not in base64? Jul 16, 2020 at 6:16
  • That is the decoded data.
    – tripleee
    Jul 16, 2020 at 6:24

3 Answers 3

2

Considering that you have loaded the json data into an object. You can then try the following:

import json
import base64

json_obj = {
    "equno": "229151246954324320",
    "data": "CdwL703m6njjyJ7tdEXdRoYKKv49oatv9UWvdLONpWgd407fakXNfbt18j+qKb/toYgHZvj34ig7iu4XN7BfrqTf/wNsIdVNJ67cH9hCyIfULWLgyT01vQ7u5zS5vSB9hazXTQHRYGxpV+eCrVFMEdxHId54sJDn+KL8hea93WyQKlFwMDO0QQD5X2lK02Y88uI7MehBUFXzK5jNTgKmLtUE9KoM4xF6bEzm2oNMrxz0QwOB4b/tvRvhThb/r+Wgb32gV3UBZBZ/RP4ID+lc7JE7TLVRyeGCNA+uV11/no358XZdGo/E5Aq7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))
5
  • I did that and I got this result: b'\t\xdc\x0b\xefM\xe6\xeax\xe3\xc8\x9e\xedtE\xddF\x86\n*\xfe=\xa1\xabo\xf5E\xaft\xb3\x8d\xa5h\x1d\xe3N\xdfjE\xcd}\xbbu\xf2?\xaa)\xbf\xed\xa1\x88\x07f\xf8\xf7\xe2(;\x8a\xee\x177\xb0_\xae\xa4\xdf\x ... Jul 16, 2020 at 6:20
  • So maybe the value is not in base64? Jul 16, 2020 at 6:21
  • @HugoZárate Is the provided data key value encoded in base 64 originally that we are trying to decode? Jul 16, 2020 at 6:22
  • Again, that is the data you wanted to decode. If it was plain text, why would it need to be base64-encoded?
    – tripleee
    Jul 16, 2020 at 6:25
  • @HugoZárate The key "data" value is in valid base-64, you can verify it on this link: base64.guru/tools/validator. You may also decode it using: base64.guru/converter/decode Jul 16, 2020 at 6:30
1

Here's a way to do that in Python:

import base64
import json
with open("sample_data.json") as f: 
    text = f.read()
    d = json.loads(text)
    data = base64.b64decode(d["data"])

The variable data now contains the decoded content of the relevant item in the json file.

0

If the string in .data were a valid base64 encoding of a UTF-8 string, the following would be equivalent:

jq -r .data input.json | base64 -D

and

jq -r '.data|@base64d' input.json

As it happens, with the given JSON, base64 shows there is a problem:

$ jq -r .data input.json | base64 -D
Invalid character in input stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.