-3

I'm using python 2.7 to parse value in JSON format there is my script:

from collections import OrderedDict
import json
import os
import pickle
d=OrderedDict([(3215, False), (3216, False), (8004, False), (8002, False),         (3589, False), (3753, False), (3752, True), (3755, False), (3754, False)]))
json_format = json.dumps(d.items())
Fichier =open('C:\\Users\\Dev\\Desktop\\file\\json.json','wb')
pickle.dump(json_format,Fichier)
Fichier =open('C:\\Users\\Dev\\Desktop\\file\\json.json','rb')
final_result_from_fichier = pickle.load(Fichier)

but after executing this script i found some external character the json file there is the output of my script :

S'[[3215, false], [3216, false], [8004, false], [8002, false], [3589, false], [3753, false], [3752, True], [3755, false], [3754, false]]'p0

What can I do ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
bader
  • 1

1 Answers1

2

You double-encoded the dictionary to JSON, then to pickle, in the file:

json_format = json.dumps(d.items())
Fichier =open('C:\\Users\\Dev\\Desktop\\file\\json.json','wb')
pickle.dump(json_format,Fichier)

That's writing the json_format string to the open file object, as Pickle encoded data. Even if you corrected this, the JSON format is not a Python pickle format. You can't load JSON with the pickle module. Just use json.load() to do so.

Use just json.dump() with the dictionary:

with open('C:\\Users\\Dev\\Desktop\\file\\json.json', 'wb') as Fichier:
    json.dump(d, Fichier)

if you want a JSON object (key-value pairs), or use d.items() if you need a JSON array containing nested arrays each with a key and a value:

with open('C:\\Users\\Dev\\Desktop\\file\\json.json', 'wb') as Fichier:
    json.dump(d.items(), Fichier)

I opened the files as a context manager to ensure it is closed again after the write operation completes.

You can load the JSON object (first snippet) back into an OrderedDict with:

with open('C:\\Users\\Dev\\Desktop\\file\\json.json','rb') as Fichier:
    final_result_from_fischier = json.load(Fichier, object_pairs_hook=OrderedDict)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • @bader: ah, sorry, my apologies. I have corrected the answer; I accidentally copied your `pickle` lines instead of correcting them to `json.dump` and `json.load` calls. Mea Culpa! – Martijn Pieters Jun 06 '16 at 11:35
  • it is worked it is worked thanks, thanks very much :) – bader Jun 06 '16 at 11:46
  • i have other question why i don' find the same forma like the list d ?? when i open the file i find:{"3215": false, "3216": false, "8004": false, "8002": false} – bader Jun 06 '16 at 11:51
  • Not sure what you are asking? – Martijn Pieters Jun 06 '16 at 11:51
  • when i open the file i find :{"3215": false, "3216": false, "8004": false, "8002": false} why didn't find list like this: [(3215, False), (3216, False), (8004, False), (8002, False)] ??? – bader Jun 06 '16 at 11:56
  • @bader: you can't have that; you can have `[[3215, false], [3216, false], [8004, false], [8002, false]]`; just write `d.items()` to the JSON file; the JSON format only supports arrays (square brackets), and booleans are always all-lowercase in JSON. Loading that back into an `OrderedDict` requires that you pass the data you load from the file into a `OrderedDict` call. – Martijn Pieters Jun 06 '16 at 11:58
  • ahhhh i'm so sorry i forgot to copied d.items() ,Thanks for your help :) – bader Jun 06 '16 at 12:05
  • Consider posting a new question post then. – Martijn Pieters Jun 07 '16 at 11:26
  • sorry for this disturbance,but i Have other question :/ Now i want to use this file in template of Django how can i call his directory !! i have been changed the directory of my file in directory of my project,i used – bader Jun 07 '16 at 11:32
  • @bader: use a JSONResponse in Django: [Creating a JSON response using Django and Python](http://stackoverflow.com/q/2428092) – Martijn Pieters Jun 07 '16 at 11:40
  • my json file had a dynamic changes,so i want to access directly to his directory and i want to use setInteval(function,1000) without reload the page HTML – bader Jun 07 '16 at 11:54
  • this is the solution :$.getJSON('/static/json.json',function(data){} thanks :) – bader Jun 08 '16 at 12:13