Coder Social home page Coder Social logo

Comments (10)

intelliapps avatar intelliapps commented on July 18, 2024

Clearly there is an issue with the actual JSON but do you care whether the output is formatted, give that an IDE or other tool can format JSON to make it more readable when that is required?

from json-csv.

ozdemirburak avatar ozdemirburak commented on July 18, 2024

No, formatting is not a problem actually, just setting the encoding option in the end like below will also take care of it.

$csv->setConversionKey('options', JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);

from json-csv.

intelliapps avatar intelliapps commented on July 18, 2024

Is it safe to assume that the JSON key names in the original input will never contain the underscore character? Otherwise I don't think the can be fully resolved by replacing convertAndSave()?

from json-csv.

intelliapps avatar intelliapps commented on July 18, 2024

Perhaps the library could be modified so that users can specify their own delimiter char/string for joining / splitting the csv column names, based on their knowledge of the json they are passing in? The potential issue with this is what characters it is acceptable to put into CSV column names, although I don't think there is a definitive specification for that.

from json-csv.

ozdemirburak avatar ozdemirburak commented on July 18, 2024

This is a great question, and I have not thought about it before actually. I think instead of forcing to join with underscore, developer should decide what operator will be used.

Maybe, this line can be changed like below.

$result = array_merge($result, $this->flatten($value, $prefix . $key . $this->conversion['join']));

from json-csv.

intelliapps avatar intelliapps commented on July 18, 2024

Great minds think alike ;0)

from json-csv.

ozdemirburak avatar ozdemirburak commented on July 18, 2024

This was funny and impressive actually :)

from json-csv.

intelliapps avatar intelliapps commented on July 18, 2024

I'll have a look at fixing this, if that's ok?

from json-csv.

ozdemirburak avatar ozdemirburak commented on July 18, 2024

Sure, it will be great, thanks.

from json-csv.

intelliapps avatar intelliapps commented on July 18, 2024

As per the comments on my pull request: I've implemented changes to get the output of csv->json to match the input to json->csv. The result is a significant improvement but there are still issues which I don't think can be resolved without changing the conversion from json to csv, or at least without further consideration.

  1. Previously, the json output represented all values as strings, wrapped in double quotes. As the example input above contains numeric values, along with most of the other test data, this resulted in a mismatch between the input and output json. I have resolved this by adding a 'numbers' conversion option which defaults to 'strings' but when set to 'numbers' causes all numeric strings to be converted to numeric types. This could still result in mis-matching input / output json where:
    a) input contains numeric f.p. values with 0 fractional value, e.g. 6.0 is output as 6
    b) input contains a mix of numbers and numeric strings

  2. With the stats data, the name of each object from the stats.json file is lost when it is converted to csv, e.g. "kemal-kilicdaroglu" and "muharrem-ince". This is consistent with the stored stats.csv test file, which does not contain these values. Since these values are not in the csv at all I can't even consider how to reinstate them into the output json to make it match the input. A related issue is that the outer braces are then {} where they should be [].

  3. With the stats data the "avg" values from the input json are rounded to fewer digits during the json to csv conversion, which means that it is not possible for the values in the output json to match.

from json-csv.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.