josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
923 stars 111 forks source link

Inquiry About JSON Query Script for Removing Time Information #500

Open jemid21 opened 1 day ago

jemid21 commented 1 day ago

Dear I hope this message finds you well.

I am a regular user of your online JSON editor and appreciate the functionality it offers. I am currently working with JSON files that include date and time information, and I need to remove the time portion while keeping only the date.

Would you happen to have or know of a specific JSON Query script that could accomplish this task? If not, could you guide me on how to write such a script or modify existing data to achieve this goal?

Your assistance would be greatly appreciated, as it will help streamline my workflow.

Thank you in advance for your support, and I look forward to hearing from you.

Best regards,

[planning_sorted 09.53.39.json](https://github.com/user-attachments/files/17784074/planning_sorted.09.53.39.json)

josdejong commented 1 day ago

Good question!

The JSON Query language does not (yet) have support for manipulating strings or work with dates.

What you can do is switch to JavaScript+Lodash via the toggle top right of the Transform Modal, see docs:

transform_switch_query_language COCD6UuP_2wBK4T

When using JavaScript as query language, you have all freedom to do stuff with strings and dates, so in case of an ISO date string you can pick just the date part using substring: "2024-11-16T10:30:01.001Z".substring(0, 10)" which returns "2024-11-16".

jemid21 commented 22 hours ago

Hello,

Thank you very much for your time and for the suggestion! I tried implementing your approach using JavaScript+Lodash in the Transform modal but unfortunately, I couldn’t get it to work. I also sought help from ChatGPT, and here’s the code I tried based on the recommendations:

// Verify the JSON is a valid object if (typeof json === 'object' && json !== null) { // Transform the date keys to remove the time for (let doctor in json) { let transformedDates = {}; for (let dateTime in json[doctor]) { // Keep only the date part (YYYY-MM-DD) const dateOnly = dateTime.split(' ')[0]; transformedDates[dateOnly] = json[doctor][dateTime]; } json[doctor] = transformedDates; // Replace with the new date keys } return json; // Return the transformed JSON } else { throw new Error("The loaded JSON is not a valid object."); }

I ensured to switch to JavaScript+Lodash mode as per your instructions, but I kept encountering errors or issues with the transformation. Could you kindly guide me further or let me know if I misunderstood something?

Thank you again for your help, and I really appreciate your time!

Best regards,

Capture d’écran 2024-11-17 à 09 37 17