Format a JSON file using clang-format

I'm facing an issue with my json file formatting. When I run clang-format on it, the output is ugly as it treats it like code.

{
  "name" : "My great app",
           "description" : "It's really cool.",
                           "version" : "0.0.1"
}

However, if I add 'foo = ' at the beginning of the file, the formatting looks great, but the structure is no longer recognized as json.

foo = {
  "name" : "My great app",
  "description" : "It's really cool.",
  "version" : "0.0.1"
}

Does anyone know how to make clang-format format the bare object in the json file like in the second example?

Answer №1

I've been enjoying using a program called jq. It's user-friendly and has excellent documentation. One cool feature is the ability to easily reformat data, like so:

jq . test.json

Answer №2

I have been focused on getting this particular task approved, implementing the changes outlined in . The solution involves inserting a hidden ""x = "" at the beginning of the file and then removing it after formatting using the replacement mechanism.

While we await approval for this approach, you may consider adopting a similar strategy with clang-apply-replacements.

Answer №3

In my personal opinion, I would recommend using Python to achieve this task. You can utilize the Python json package's pretty printer for this purpose:

cat mydata.json | python -mjson.tool

If you want to customize the output further:

cat mydata.json | python -c 'import json, sys; print(json.dumps(json.load(sys.stdin), indent=4, sort_keys=True))'

On a different note, if I don't have access to clang-format, I prefer using an existing tool for better formatting.

It's worth mentioning that you can also achieve this within vim by using the == normal command on the entire file selection ☺

Answer №4

For those with the json_pp tool installed, another option is:

cat myfile.json | json_pp

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Showing only the objects that are marked as true in the component (React)

I have implemented two key React components. One of them is VideoGameList.js, it serves as an export of an array containing multiple objects. const VideoGameList = [ { id: 1, title: "Fire Emblem Engage", src: vg01, releaseDate: ...

Having trouble resolving a setInterval problem with JavaScript?

Yesterday, I learned about the setInterval function which allows me to execute a task or function after a specific amount of time. While I have successfully implemented the interval in my code, it keeps adding new lines with the date each time. What I re ...

Convenient Method for Making POST Requests with the Node Request Module and Callback

Does the .post() convenience method in Javascript/Node's request module accept a callback? I'm confused why it would be throwing an error like this: var request = require('request'); request.post({url: 'https://identity.api.foo/v ...

Is it possible to increase Google+ shares without needing an API key for every single page?

Looking to retrieve the number of Google+ shares for each URL on a search results page using Ajax. Facebook and Twitter share counts have been successfully implemented: $.getJSON('http://urls.api.twitter.com/1/urls/count.json?url=' + url + & ...

Learning to parse a JSON file in Java

I need assistance with reading a Json file. The content of my Json File is as follows: [ { "arguments" : [ { "IsEnabled" : "false", "class" : "UITextField", "width" : 238, "parent" : { "class" : "UIVie ...

Utilizing Objects as Properties in Phaser.Scene in Phaser 3

I've just started working with Phaser using TypeScript and I'm facing an issue. I attempted to move my main objects out of the create and preload methods by loading them as Phaser.Scene class properties. However, after making this change, my game ...

Is it possible to impose a different style on an element from another culture?

I am currently developing a themes library along with a demo page. The challenge I'm facing is that the demo page needs to showcase styles from the library without using all of the elements. For instance, consider the following style in an external s ...

Using Google App Script to transfer specific columns of a row to a different tab based on the value in a particular column

I have a script that moves rows based on a specific value in a column, but I am looking to only transfer certain columns within those rows. This is the current script I am using: //Script to move rows from Form tab to Des tab function moveSafeRows() { v ...

Verification of user input field

For this mini deposit app, I needed to implement validation for the input field to check for three different conditions: no blank entries, only numerical values, and no negative numbers. Despite having the functionality, my attempts at implementing validat ...

When working with React.js, encountering the error message "Unexpected token export on export{default as

When attempting to import components into my newly installed app, I used the following syntax: import {Cards , Chart , CountryPicker} from '../components' I also created an index.js file with the following content: export {default as Cards} ...

Encountering issues with running `npm start` following the creation of a fresh React project using create

Encountering an issue with react-scripts after setting up a new react project. I initiated the project using npx create-react-app dashboard. Upon entering the dashboard directory and executing npm start (without any prior actions), the following error is d ...

error encountered during module import in Ember.js

I am struggling with removing diacritics from a string in my Ember.js app. After discovering the 'fold-to-ascii' plugin on GitHub, I added it to my project using npm. Although the plugin is visible under the node_modules folder, I'm unsure o ...

The scroll bar will be automatically reset once the content inside is updated by using the $selector.html() function

I am looking to create a dynamic scrollable content feature. Initially, I retrieve the data and set the content as follows: $("#sub_menu li").on("click", function () { $("#gallery").html(get_html($(this).attr("id"))); $("#gallery").cs ...

Turn JSON logic into an Elasticsearch query

Prior to dedicating time to constructing this tool, I am curious if anyone has developed or is familiar with a conversion tool that can convert jsonlogic (jsonlogic.com) into an elasticsearch query dsl format. Appreciate any insights! ...

Interested in creating a weather application that changes color based on the time of day

My attempt to create a weather app with a glowing effect has hit a roadblock. I want the app to glow "yellow" between 6 and 16 hours, and "purple" outside of those hours. I have assigned classes for AM and PM elements in HTML, and styled them accordingly i ...

What is the best approach for sending data from multiple tables via an ajax request?

I've encountered an issue where the controller isn't receiving any data when I try to send data using jQuery. My goal is to post data from two tables, with dynamically created fields and some located in the HTML. Below is a snippet of my code: ...

Sending extensive data using HTTParty

Currently, my approach involves using HTTParty to send data to a server through the code snippet below: this_component = {"name" => "something", "ip" => "localhost", "logs" => logs_to_push} payload = {"payload" => JSON.dump(this_component)} re ...

I tried implementing a progress bar for my JSON post and response, but unfortunately, it is not displaying as expected

I have successfully implemented JSON data posting to the server. However, I am facing an issue with integrating a progress bar to show the progress. The progress bar is not displaying at all despite the data being posted and response received. @Override ...

What methods can be used to transmit binary information from a Node.js socket.io server to a web browser?

After extensively searching the Socket.IO documentation, I am still unable to locate a straightforward example of how to send binary data between a server and client. Despite their assurances that it is included, I have yet to find a minimal demonstration. ...

What is the best way to personalize Material UI elements, such as getting rid of the blue outline on the Select component?

Embarking on my journey of developing a React app, I made the decision to incorporate Material UI for its array of pre-built components. However, delving into the customization of these components and their styles has proven to be quite challenging for me ...