0

I have a.json file with the following content

{"created_at": "Fri Feb 01 03:19:49 +0000 2019", "id": 1091174254324928512, "id_str": "1091174254324928512", "text": "\u091c\u094b \u091c\u0940\u0902\u0926 \u0915\u093e \u092e\u0942\u0921 \u0939\u0948, \u0935\u0939\u0940 \u0939\u093f\u0928\u094d\u0926 \u0915\u093e \u092e\u0942\u0921 \u0939\u0948 \u270c\ufe0f\n\n#JindByPollResults #JindByelection\n#jindbypoll #JindResults\n#fridayfeelings", "truncated": false, "entities": {"hashtags": [{"text": "JindByPollResults", "indices": [43, 61]}, {"text": "JindByelection", "indices": [62, 77]}, {"text": "jindbypoll", "indices": [78, 89]}, {"text": "JindResults", "indices": [90, 102]}, {"text": "fridayfeelings", "indices": [103, 118]}], "symbols": [], "user_mentions": [], "urls": []}, "source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>", "in_reply_to_status_id": null, "in_reply_to_status_id_str": null, "in_reply_to_user_id": null, "in_reply_to_user_id_str": null, "in_reply_to_screen_name": null, "user": {"id": 439154852, "id_str": "439154852", "name": "Chowkidar Kailash Vijayvargiya", "screen_name": "KailashOnline", "location": "Delhi", "description": "National General Secretary of #BJP, Active Worker & a Proud Indian who continuously strives for development.", "url": "https://twitter.com/KbWcYpNlEZ", "entities": {"url": {"urls": [{"url": "https://twitter.com/KbWcYpNlEZ", "expanded_url": "http://www.KailashOnline.in", "display_url": "KailashOnline.in", "indices": [0, 23]}]}, "description": {"urls": []}}, "protected": false, "followers_count": 756260, "friends_count": 1777, "listed_count": 243, "created_at": "Sat Dec 17 12:36:56 +0000 2011", "favourites_count": 7206, "utc_offset": null, "time_zone": null, "geo_enabled": true, "verified": true, "statuses_count": 17644, "lang": "en", "contributors_enabled": false, "is_translator": false, "is_translation_enabled": false, "profile_background_color": "000000", "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile": true, "profile_image_url": "http://pbs.twimg.com/profile_images/1073134305466114048/Kx-Y7xkk_normal.jpg", "profile_image_url_https": "https://pbs.twimg.com/profile_images/1073134305466114048/Kx-Y7xkk_normal.jpg", "profile_banner_url": "https://pbs.twimg.com/profile_banners/439154852/1544690191", "profile_link_color": "E06412", "profile_sidebar_border_color": "000000", "profile_sidebar_fill_color": "DDEEF6", "profile_text_color": "333333", "profile_use_background_image": true, "has_extended_profile": true, "default_profile": false, "default_profile_image": false, "following": false, "follow_request_sent": false, "notifications": false, "translator_type": "none"}, "geo": null, "coordinates": null, "place": null, "contributors": null, "is_quote_status": false, "retweet_count": 663, "favorite_count": 2705, "favorited": false, "retweeted": false, "lang": "hi"}

As you can see, the id field value is = 1091174254324928512

In python3, with original a.json

>>> import json
>>> f=open("a.json","r")
>>> d=json.load(f)
>>> d["id"]
1091174254324928512

When I print the same file with the command cat a.json | jq .
I get the following output

{
  "created_at": "Fri Feb 01 03:19:49 +0000 2019",
  "id": 1091174254324928500,
  "id_str": "1091174254324928512",
  "text": "जो जींद का मूड है, वही हिन्द का मूड है ✌️\n\n#JindByPollResults #JindByelection\n#jindbypoll #JindResults\n#fridayfeelings",
  "truncated": false,
  "entities": {
    "hashtags": [
      {
        "text": "JindByPollResults",
        "indices": [
          43,
          61
        ]
      },
      {
        "text": "JindByelection",
        "indices": [
          62,
          77
        ]
      },
      {
        "text": "jindbypoll",
        "indices": [
          78,
          89
        ]
      },
      {
        "text": "JindResults",
        "indices": [
          90,
          102
        ]
      },
      {
        "text": "fridayfeelings",
        "indices": [
          103,
          118
        ]
      }
    ],
    "symbols": [],
    "user_mentions": [],
    "urls": []
  },
  "source": "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>",
  "in_reply_to_status_id": null,
  "in_reply_to_status_id_str": null,
  "in_reply_to_user_id": null,
  "in_reply_to_user_id_str": null,
  "in_reply_to_screen_name": null,
  "user": {
    "id": 439154852,
    "id_str": "439154852",
    "name": "Chowkidar Kailash Vijayvargiya",
    "screen_name": "KailashOnline",
    "location": "Delhi",
    "description": "National General Secretary of #BJP, Active Worker & a Proud Indian who continuously strives for development.",
    "url": "https://twitter.com/KbWcYpNlEZ",
    "entities": {
      "url": {
        "urls": [
          {
            "url": "https://twitter.com/KbWcYpNlEZ",
            "expanded_url": "http://www.KailashOnline.in",
            "display_url": "KailashOnline.in",
            "indices": [
              0,
              23
            ]
          }
        ]
      },
      "description": {
        "urls": []
      }
    },
    "protected": false,
    "followers_count": 756260,
    "friends_count": 1777,
    "listed_count": 243,
    "created_at": "Sat Dec 17 12:36:56 +0000 2011",
    "favourites_count": 7206,
    "utc_offset": null,
    "time_zone": null,
    "geo_enabled": true,
    "verified": true,
    "statuses_count": 17644,
    "lang": "en",
    "contributors_enabled": false,
    "is_translator": false,
    "is_translation_enabled": false,
    "profile_background_color": "000000",
    "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
    "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
    "profile_background_tile": true,
    "profile_image_url": "http://pbs.twimg.com/profile_images/1073134305466114048/Kx-Y7xkk_normal.jpg",
    "profile_image_url_https": "https://pbs.twimg.com/profile_images/1073134305466114048/Kx-Y7xkk_normal.jpg",
    "profile_banner_url": "https://pbs.twimg.com/profile_banners/439154852/1544690191",
    "profile_link_color": "E06412",
    "profile_sidebar_border_color": "000000",
    "profile_sidebar_fill_color": "DDEEF6",
    "profile_text_color": "333333",
    "profile_use_background_image": true,
    "has_extended_profile": true,
    "default_profile": false,
    "default_profile_image": false,
    "following": false,
    "follow_request_sent": false,
    "notifications": false,
    "translator_type": "none"
  },
  "geo": null,
  "coordinates": null,
  "place": null,
  "contributors": null,
  "is_quote_status": false,
  "retweet_count": 663,
  "favorite_count": 2705,
  "favorited": false,
  "retweeted": false,
  "lang": "hi"
}

here id field value that is printed is = 1091174254324928500 which is different from id field value that is present in the file.

In python3, with prettified pretty_a.json

>>> import json
>>> f=open("pretty_a.json","r")
>>> d=json.load(f)
>>> d["id"]
1091174254324928500

Also, When I opened a.json with VSCode and used prettify_JSON extension, then I get the same prettified output as that of what is printed with cat a.json | jq .

So I think the problem is because of how json decoders handles number data type.
It seems like json decoders of jq and python3 handle datatypes differently.

My question is why exactly this happens and how to handle these type of peculiarities while coding with json module of python3 irrespective of whether the given json file is jq prettified or not.

jaggi
  • 357
  • 1
  • 4
  • 17
  • 2
    see **Numbers** section in [jq FAQ](https://github.com/stedolan/jq/wiki/FAQ#numbers). *The jq parser converts JSON numbers to IEEE 754 64-bit values. The original JSON representation is lost...* – oguz ismail Apr 28 '19 at 07:51
  • https://tools.ietf.org/html/rfc8259#section-6 – melpomene Apr 28 '19 at 08:32
  • 1
    There is no "`int` data type" in JSON, only numbers, the interpretation of which depends on the implementation. Your a.json file is non-portable. – melpomene Apr 28 '19 at 08:33

0 Answers0