0

i have this file JSON:

And i want read this file in python, but this code report one error.

{ tipoGrafico: 'Multi Serie Chart',
  min_value: '1',
  max_value: '1',
  min_strategy: '2',
  max_strategy: '3',
  mutation: '4',
  cxpb: '5',
  mutpb: '6',
  value_tournSize: '7',
  pop_size: '100' 
}

my code python:

import json
import sys
print("nome del json: ",sys.argv[1])
data = json.load(open(sys.argv[1]))
data["tipoGrafico"]

but i have this error:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
blhsing
  • 91,368
  • 6
  • 71
  • 106
Difettoso
  • 51
  • 1
  • 1
  • 10

4 Answers4

1

Your file is not a valid JSON file - see Single vs double quotes in JSON and JSON standard. In fact, it's not even a valid python dictionary, since keys are not enclosed in any quotes.

For your file to be read, you'll need to change it, like this:

{ "tipoGrafico": "Multi Serie Chart",
  "min_value": "1",
  "max_value": "1",
  "min_strategy": "2",
... }

Also, to ensure proper file handling and closing, I would recommend to use the with statement when opening your file:

with open(sys.argv[1]) as json_file:
    data = json.load(json_file)
data["tipoGrafico"]
mfrackowiak
  • 1,294
  • 8
  • 11
1

This "JSON" is actually valid YAML, so you can simply load it with the yaml module instead (after installing the pyyaml package):

import yaml
import sys
print("nome del yaml: ",sys.argv[1])
data = yaml.load(open(sys.argv[1]))
data["tipoGrafico"]
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

I take this "json" from my application with this js:

$(document).ready(function() {
    $("#submit").on('click', function(){
        // send ajax
        $.ajax({

            url: 'http://127.0.0.1:8081/', // url where to submit the request
            type : "POST", // type of action POST || GET
            dataType : 'json', // data type
            data : $("#form").serialize(), // post data || get data
            success : function(result) {
                // you can see the result from the console
                // tab of the developer tools
                console.log(result);
            },
            error: function(xhr, resp, text) {
                console.log(xhr, resp, text);
                }
            })
        });
});

on this server node:

var express = require("express");
var bodyParser = require("body-parser");

var app = express();
var jsonParser = bodyParser.json();
var urlencodedParser = bodyParser.urlencoded({ extended: false });


var obj = {
    table: []
};

app.post("/", urlencodedParser, function(request, response) {   
    console.log(request.body); //This prints the JSON document received (if it is a JSON document)
    obj.table.push(request.body);
});

var json = JSON.stringify(obj);

var fs = require('fs');
fs.writeFile('myjsonfile.json', json, 'utf8');

//Start the server and make it listen for connections on port 8080
app.listen(8081);

how to take the correct json file?

Difettoso
  • 51
  • 1
  • 1
  • 10
0


that is not a valid JSON.
Try with this:

{
    "tipoGrafico": "Multi Serie Chart",
    "min_value": "1",
    "max_value": "1",
    "min_strategy": "2",
    "max_strategy": "3",
    "mutation": "4",
    "cxpb": "5",
    "mutpb": "6",
    "value_tournSize": "7",
    "pop_size": "100"
}
escattoni
  • 1
  • 1
  • 1