Using jq: Only select parents with a certain child key

Example json:

Example json:

{
  "version": "3",
  "services": {
    "web": {
      "build": "web"
    },
    "redis": {
      "image": "redis"
    },
    "datadog": {
      "build": "datadog"
    },
    "another": {
      "image": "mysql"
    }
  }
}

I'd like to return a list of services that have the "build" key, and not the "image" key. Note that the value for the build key isn't something I can key off of.

Output should be: ["web", "datadog"]

JSON Tutorial For Beginners | What is JSON | Learning JSON with JavaScript

JSON Tutorial For Beginners | What is JSON | Learning JSON with JavaScript

JSON Tutorial For Beginners | What is JSON | Learning JSON with JavaScript

Explore JSON and how JavaScript Objects can be used to access data within JSON data format and output to your web page

Guide to learning how to use JavaScript Objects and JSON data. JSON is the most popular format for data exchange between applications. If you are interested in connected to a web API chances are its JSON formatted. Learn to use AJAX to connect and bring JSON data into your JavaScript!

This course shows you how to work with JSON formatted data, output content, loop JSON data, Parse JSON and a whole lot more.

JSON (JavaScript Object Notation) is a syntax for data. JSON is easier to use than XML and human readable. Most modern web APIs output data in JSON formats. It's a lightweight data interchange format that is quickly becoming the default format for data exchange on internet today! JSON is lightweight, language independent and easy to read and write. JSON is better than XML and more popular!

Within the lessons of this course we will explore

  • JavaScript data types used to hold variables and how they work
  • JSON and how to write JSON data
  • How to add values into a JSON object
  • Accessing JSON data and bringing it into JavaScript
  • JavaScript JSON parse and stringify methods
  • Adding JSON to local storage
  • Retrieving back data within JSON formats, updating and working with JSON
  • Connecting to a web API using fetch
  • Retrieving JSON data from a web API and outputting the results into your web page
  • Iterating threw multiple results from an API
  • Google Spreadsheet data as JSON and how to practice retrieving data
  • All of the source code and resources are in included
  • Explore different methods of working with the JSON data stringify and parsing
  • How JavaScript objects can use Arrays to hold multiple items
  • How JavaScript arrays work and store data

Thanks for reading

If you liked this post, please do share/like it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about JavaScript and JSON

The Complete JavaScript Course 2019: Build Real Projects!

JavaScript Programming Tutorial | Full JavaScript Course for Beginners 2019

The complete beginner’s guide to JSON

The Complete Guide to JSON Web Tokens

JWT Fundamentals for Beginners

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

New ES2019 Features Every JavaScript Developer Should Know

How to use JSON.stringify() and JSON.parse() in JavaScript

How to use JSON.stringify() and JSON.parse() in JavaScript

JSON.stringify() and JSON.parse() are useful tools for handling JSON-formatted content

There are two useful methods to deal with JSON-formatted content: JSON.stringify() & JSON.parse(), which are useful to learn as a pair.

JSON.parse() takes a JSON string and then transforms it into a JavaScript object.

const myObject = {
  dog: "🐕",
  cat: "🐈",
  koala: "🐨",
  count: 3
};

console.log(JSON.stringify(myObject));
// result: {"dog":"🐕","cat":"🐈","koala":"🐨","count":3}

console.log(JSON.parse(JSON.stringify(myObject)));
// result: Object {dog: "🐕", cat: "🐈", koala: "🐨", count: 3}

JSON.stringify() can take arguments, a replacer function and a string or number to use as a “space” in the returned string.

That means the replacer argument can be used to filter out values if you return undefined, as shown in the following example:

const user = {
  id: 101010,
  name: "Derek",
  email: "[email protected]"
};

function replacer(key, value) {
  if (typeof value === "number") {
    return undefined;
  }
  if (key === "email") {
    return "Removed for privacy";
  }
  return value;
}

console.log(JSON.stringify(user));
// result: {"id":101010,"name":"Derek","email":"[email protected]"}

console.log(JSON.stringify(user, replacer));
// {"name":"Derek","email":"Removed for privacy"}

console.log(JSON.stringify(user, null, "^_^ "));
// result: {
// ^_^ "id": 101010,
// ^_^ "name": "Derek",
// ^_^ "email": "[email protected]"
// }

console.log(JSON.parse(JSON.stringify(user)));
// result: Object {id: 101010, name: "Derek", email: "[email protected]"}

How do you spell stringify backwards? JSON.parse() (Photo by clement fusil on Unsplash)

how to build a new json string from existing json string

I have multiple files that contain json data that looks like this:

I have multiple files that contain json data that looks like this:

1.json:

[{
    "id": 2100059,
    "email": "",
    "company": "acme",
    "job_title": "",
    "first_name": "Jane",
    "last_name": "Doe"
 }, 
 {
    "id": 2199991,
    "email": "",
    "company": "Widgets Inc",
    "job_title": "",
    "first_name": "John",
    "last_name": "Doe"
 }]

2.json:

[{
    "id": 123456,
    "email": "",
    "company": "acme",
    "job_title": "",
    "first_name": "GI Jame",
    "last_name": "Adf"
 }, 
 {
    "id": 000001,
    "email": "",
    "company": "Widgets Inc",
    "job_title": "",
    "first_name": "bob",
    "last_name": "billy"
 }]

How can I combine these two lists so that it's just one string, with one set of []?

So far, I'm writing the logic read in the contents of each file into separate strings. But then I'm thinking I should: 1. json_decode() each string to treat it like an object

Not sure how to append to the object though. any tips would be appreciated.

EDIT 1

I decided to read each file contents into an array, and then try the tips suggested below to use array_merge. when i do a print_r on my array, it looks like this: (bogus data, but you should get the idea)

Array
(
    [0] => [{"id":2100059,"email":"","company":""},{"id":2129527,"email":"","company":""},{"id":2134804,"email":"","company":""},{"id":2148239,"email":"","company":""}]
    [1] => [{"id":2100059,"email":"","company":""},{"id":2129527,"email":"","company":""},{"id":2134804,"email":"","company":""},{"id":2148239,"email":"","company":""}]
[2] => [{"id":2100059,"email":"","company":""},{"id":2129527,"email":"","company":""},{"id":2134804,"email":"","company":""},{"id":2148239,"email":"","company":""}
[3] =>[{"id":2100059,"email":"","company":""},{"id":2129527,"email":"","company":""},{"id":2134804,"email":"","company":""},{"id":2148239,"email":"","company":""}]

)

And then this is the logic I have that tries to combine all this into one json string:

    print_r($allpages);
$finaljsonstring ='';
foreach ($allpages as $item)
{
$finaljsonstring = $finaljsonstring + json_encode(array_merge(json_decode($item)));
}
echo $finaljsonstring;

But something is going wrong. (I'm calling this as ajax so it's hard to debug) but when i console.log the results from the ajax call, nothing is returned. Ultimately, what I need to return to the front end is a single array of json strings like this:

[{}, {},{}]